Agents get stuck. A test suite waits on a network call that will never answer. A model writes while True: and forgets the break. A build pulls in a dependency that compiles for far longer than anyone expected. None of this is malicious, and all of it costs time and money if nothing stops it.

The fix is not one timeout. It is three, at different layers, each catching a different failure. This post explains the three clocks around a Sandscale sandbox, the defaults, and how to choose values for an agent loop. The same structure applies to any sandbox you run agent code in.

Clock 1: how long the sandbox lives

Every sandbox has a lifetime. When it runs out, the sandbox stops by itself, whatever is happening inside it.

In the TypeScript SDK this is timeoutSeconds on sandboxes.create. In the REST API it is ttl_seconds when you start an instance. According to the TypeScript SDK reference, the server default is 3600 seconds and the maximum is 86400.

import { Sandscale } from '@sandscale/sdk'; const sandscale = new Sandscale(); // reads SANDSCALE_API_KEY const box = await sandscale.sandboxes.create({ template: 'builtin:python', timeoutSeconds: 20 * 60, // this sandbox stops itself after 20 minutes labels: { task: 'fix-flaky-test' }, }); 

This clock is your backstop. It catches the failures your own code never sees: your worker crashes after creating a sandbox, a deploy restarts the process that owned it, someone hits Ctrl-C in the middle of a run. Without a lifetime, those sandboxes keep running until somebody notices. With one, they clean themselves up.

Plan limits sit on top of this: each plan has a maximum session length, listed on the pricing page, so keep the lifetime within your plan's limit.

How to choose it: take the longest a whole task should ever take, from first command to last, and add a margin. If a typical task finishes in a few minutes, a lifetime of 20 or 30 minutes is generous and still bounds the damage. Do not set the maximum "just in case". The point of this clock is to be shorter than the time it takes a human to notice a problem.

If your agent needs a sandbox across several conversation turns with gaps in between, pausing is usually a better fit than a very long lifetime: pause keeps memory and files, and resume carries on where it stopped. See pause and resume.

Clock 2: how long one command may run

Inside a living sandbox, each command gets its own limit. In the SDK this is timeoutSeconds on sandboxes.run; in the REST API it is timeout_sec on the exec call. The default is one minute.

const result = await sandscale.sandboxes.run(box.id, ['pytest', '-x', '-q'], { region: box.region, timeoutSeconds: 300, // this command may take up to 5 minutes }); if (result.timedOut) { // tell the model its command was stopped for taking too long } else if (result.oomKilled) { // tell the model it ran out of memory } else { // result.exitCode, result.stdout and result.stderr describe what happened } 

When a command hits its limit, run does not throw. It returns a result with timedOut set to true. That is the right design for agents: a command that ran too long is information about the model's code, and the model should see it. Something like "your command was stopped after 300 seconds; the last output was ..." lets the model notice an infinite loop or a hanging network call and try something else.

How to choose it: per command type, not one global value. A quick python -c check deserves the default or less. A full test suite or a build deserves more. A good pattern is to let your tool definition expose a small set of named limits ("quick", "test", "build") rather than letting the model pick any number. Models asked to choose their own timeout tend to choose large ones.

Keep clock 2 shorter than clock 1. If a single command may run for longer than the sandbox lives, the sandbox stopping is what ends the command, and you lose the clean timedOut result.

Clock 3: how long your client waits

The third clock is the HTTP request itself. The SDK client has a per-request timeout, timeoutMs in the client options, with a default of one minute (60,000 milliseconds). When it runs out, the call fails with a ConnectionError.

There is an obvious trap here: a command allowed to run for five minutes, sent over a request that gives up after one. In @sandscale/sdk 1.0.0 the SDK handles this for you on run. It sets that request's timeout to the command's timeoutSeconds plus a margin, so the client does not give up before the server does. If you call the REST API directly, you need to do the same: your HTTP client's timeout for an exec call should be longer than the timeout_sec you send.

For long commands, streaming is often better than one long request anyway. With onStdout or onStderr set, the SDK starts the command without waiting, gets a run ID back at once, and delivers output live from that run's log stream. Your agent can show progress, and a dropped connection resumes the stream instead of losing the run.

const build = await sandscale.sandboxes.run(box.id, 'npm run build', { region: box.region, timeoutSeconds: 600, onStdout: (chunk) => process.stdout.write(chunk), onStderr: (chunk) => process.stderr.write(chunk), }); 

How the three fit together

Put the clocks in order from shortest to longest:

  1. Request timeout: covers a slow or unreachable API. Retried for reads by the SDK.
  2. Command timeout: covers a command that hangs or loops. Reported to the model as timedOut.
  3. Sandbox lifetime: covers everything else, including your own process dying. The sandbox stops itself.

And one more clock that lives in your own code: a budget for the whole agent task, counted in turns, tokens or wall time. When the agent has tried ten times to make a test pass, stopping and asking a person is usually cheaper than an eleventh attempt.

Always stop what you start

Timeouts are the safety net, not the plan. When a task finishes, stop the sandbox explicitly:

try { // ... the agent's work ... } finally { await sandscale.sandboxes.stop(box.id, { region: box.region }); } 

On Sandscale, running time counts against your plan's vCPU-hours, so a sandbox that finished its work but keeps running is spending hours on nothing. The awake-time billing post explains what counts. Explicit stops plus a short lifetime keep the bill and the blast radius small.

A starting point

If you want numbers to start from and tune later:

  • Sandbox lifetime: about 2 to 3 times the longest task you expect.
  • Command timeout: the one-minute default for quick checks; minutes for test suites and builds; never longer than the sandbox lifetime.
  • Request timeout: leave the SDK default; it adjusts for run.
  • Task budget: a fixed number of attempts before a person is asked.

Then look at the timedOut results in your run records after a week. They tell you which limits are too tight and which commands are hanging.

Originally published on the Sandscale blog.