steps docs
start here Writing pipelines resourceswebhooksexprcontrol-flowagentsattempts-timeoutworkspaceinfratemplatingmcpcomplete Reference webobservabilityauthenticationagents-internalsaws-workersgcp-workersdo-workersconformance

Agent internals

How agent steps behave underneath: transport-level repair, loop detection, provider-specific caching, retry/timeout interaction, and context compaction.

None of this is configuration you write day to day — see agents.md for that. This page is here for when something behaves in a way the authoring guide doesn't explain, or when you're changing this code.

Repairing malformed tool-call arguments

An OpenAI-compatible server sends a tool call's arguments as a JSON string, and weak local models (LM Studio, Ollama class) malformed it often: truncated at max_tokens mid-string, trailing commas, prose or markdown fences around the object. The LLM adapter parses that string leniently — on any parse failure it silently substitutes an empty map, so the call arrived at the conversation as if the model had passed no arguments at all: the tool answered "missing required argument", the model had no idea why, and a coding agent could burn most of its turn budget rediscovering the failure.

Every agent's HTTP client therefore carries a response-repair transport (internal/agent/llmclient/repair.go) that inspects each chat-completion response and, for any arguments string that doesn't parse, attempts a minimal best-effort repair before the adapter ever sees it — the same validate → repair → re-validate shape as crush/fantasy's tool-call repair, placed at the transport because that is the last place the model's raw text exists. The rule set covers only what local models actually produce: dropping prose/fences around the object, closing an unterminated string and unclosed braces (truncation), supplying null for a dangling key, and stripping trailing commas. Anything else is left exactly as-is — an unrepairable call behaves precisely as it did before this existed, so repair can only recover a call, never alter a valid one (an all-valid response passes through byte-identically, and non-chat requests are never touched).

Transport-level only, like the OpenRouter caching levers: no config surface, no merkle impact, and it applies to every provider, since malformed arguments are an OpenAI-compat-wide hazard rather than a provider-specific one.

Loop detection

max_turns bounds how long a conversation can run, but it is a blunt backstop: an agent that re-issues the identical tool call and gets the identical result — re-reading a file it has already read, re-running a command whose output cannot change — is stuck, and would spin out its entire turn budget producing nothing. The conversation loop hashes each tool interaction (tool name + arguments + result; internal/agent/loopdetect.go, copied from crush's loop_detection.go) and counts repeats within a sliding window of the last 10 tool-executing turns.

The result is part of the hash so that productive repetition never trips the detector: re-reading a file after an edit returns different bytes, and a verify command's output changes as the tree is fixed. Only call-and-result-both-identical accumulates.

The reaction is two-strike. The first time one interaction exceeds 5 copies in the window, the conversation gets a warning message naming the tool and telling the model to change approach — the window is not reset, so the warning is the model's only chance. A second detection (i.e. it repeated the same interaction once more) fails the attempt as a task failure (failed, not errored, for hook dispatch) with "agent stuck in a loop". Unlike crush's window — which only starts counting after 10 full turns — any count over the threshold triggers, so an agent with the default 30 max_turns is protected exactly like one with 200. The detector is scoped to the conversation, which runs exactly once per step.

Gateway prompt caching

An agent whose model: resolves to OpenRouter (the openrouter/ prefix, or a source.endpoint: pointing at openrouter.ai) gets two request mutations automatically. Neither is on by default in a stock OpenAI-compatible client, and without them a conversation pays full input price on every turn — exactly where caching is worth the most, since the whole prior history is re-sent each time.

Why the session is scoped per agent

OpenRouter tracks sticky routing "at the account level, per model, and per conversation" — and an agents: entry is the (model, persona, dials) bundle, so two different agents share no cacheable prefix and are not one conversation. The session key is therefore the job run plus the agent name:

Same session?Why
Turns of one step's conversation loopyesthe whole prior history is re-sent each turn — the big win
A to:/verdicts: revise loop re-entering a stepyessame agent, same prefix, across separate visits
A sub-agent called repeatedly by its parentyessame persona every call
A fix: agentyessame persona every call
Two different agents in one jobnodifferent model and persona — nothing to reuse
The same agent in two runs (incl. concurrent)nono cross-run provider pin
An attempts: request retryyesit continues the same conversation — see below

A retry keeps the pin: attempts: is a request-level retry that continues the same conversation, so the accumulated prefix is exactly what it wants to reuse. The session is stable for the life of a (run, agent), with no retry component.

The per-agent split is not merely tidy. With a router model (openrouter/auto and friends) a session pins the resolved model, not just the provider — so under a run-wide session whichever agent ran first would silently choose the concrete model for every later agent in the job.

Keying on the agent name rather than the step index is equally deliberate: scoping per step invocation would fragment exactly the revise-loop and repeated-sub-agent cases where caching pays off most.

Both mutations are transport-level only:

Other gateways

Three more gateways get the same per-agent, per-run treatment, each in its own spelling; Helicone, listed for contrast, gets neither a header nor a field. Where there is a caching switch it is sent for every model, unlike OpenRouter's marker: the gateway itself adds cache_control breakpoints only for providers that need them (Anthropic and a few others) and leaves implicit-caching providers alone. A body that already carries the field is left untouched, and a gateway with no field has its body passed through unchanged.

PrefixKeySession headerCaching field
vercel/ (ai-gateway.vercel.sh)AI_GATEWAY_API_KEYx-session-affinityproviderOptions: {gateway: {caching: auto}}
requesty/ (requesty.ai)REQUESTY_API_KEYnone documentedrequesty: {auto_cache: true}
opencode/ (opencode.ai)OPENCODE_API_KEYx-opencode-session (required)none
helicone/ (ai-gateway.helicone.ai)HELICONE_API_KEY—none

opencode is the one whose header is not an optimization. Its gateway refuses a request that arrives without one — 400 {"type":"MissingSessionID"} — and routes nothing, so every opencode/ model failed until steps sent it. It needs no caching field: it routes to providers that cache implicitly and reports prompt_cache_hit_tokens back without being asked. It takes bare model names (opencode/deepseek-v4.1-flash), not vendor/model.

That difference decides what happens outside a job run. A session is derived from the run id, and a preflight probe — steps validate --live, and the check before every job — has none. An optional header is simply left off there, which is right: there is no conversation to keep a cache warm for. A required one is synthesized instead (probe-<random>), because the alternative is a provider that preflight can never reach, failing every job before its first step. Random rather than fixed, since two concurrent probes sharing one id would ask the gateway to pin them to a single upstream instance — the opposite of what a health check wants.

Helicone has no request-wide switch: Anthropic caching there needs a cache_control marker placed on individual messages, so an Anthropic model through helicone/ pays full input price every turn. Models with implicit caching (OpenAI, Gemini, DeepSeek) are unaffected. Helicone takes bare model names (helicone/claude-sonnet-4); the others take vendor/model.

Whether caching is landing is visible in steps runs cost's CACHED column — the provider's own reported cache figures, recorded per step (see agents.md).

Timeout and Attempts on Agent Steps

Agent steps can set timeout: and attempts: to bound their execution:

- agent: reviewer
  messages:
    - "Review the PR"
  timeout: 10m
  attempts: 2

Timeout bounds the entire conversation (all turns, all tool calls, and any request retries within them) to a wall-clock deadline. The built-in agentStepTimeout is 30 minutes; timeout: (if set) overrides it. A timeout mid-conversation classifies as failed, per Concourse — the step spent its budget without concluding (see attempts-timeout.md).

Attempts retries the failing request, in llmclient/requests.go's transport. The conversation runs exactly once and carries on from where it was, so nothing accumulated is lost. Tool failures never trigger it at all — they come back to the model as data.

A task step's fix: agent can also set attempts: and timeout: independently:

- task: test
  run: ./test.sh
  attempts: 3
  fix:
    agent: fixer
    timeout: 5m
    attempts: 2

The fix agent's timeout/attempts are separate budgets — they don't consume or conflict with the task's retry count or timeout.

Why attempts: lives in a transport

internal/agent/llmclient/requests.go implements attempts: as requestRetryTransport, innermost in the client's stack. Two properties force it there.

It is the only layer that can see a request. A request is the operation attempts: names, and everything above the transport deals in conversations.

It is the only layer that can switch off the client's own retry. openai-go/v3 retries every request twice by default, and adk-utils-go's genai/openai/completions Config has no MaxRetries. A retry loop anywhere above the transport therefore multiplies with that hidden one instead of replacing it, which is exactly the attempts: × 3 problem that used to make attempts: 6 cost up to eighteen requests. From inside the transport the retry can be refused, using the two mechanisms openai-go documents:

Both paths are covered by tests in requests_test.go, and e2e_test.go pins the request count end to end — a dependency bump that breaks either mechanism fails the suite rather than silently re-pricing every failing call.

One accounting detail: three failing requests produce two agent.request_retry lines. The third failure is the one the step reports, via agent.conversation_failed with the full provider_requests count beside it; logging it in both places would double-count the number this exists to make honest.

See attempts-timeout.md for a detailed guide covering the interaction with assert:, hook firing, and other step types.

CLI-backed agents: how delegation is wired

A source.model of @claude/sonnet replaces the conversation, not the endpoint. The authoring view is in agents.md; this is the mechanism.

Where the branch lives

config.resolveAgentTarget dispatches on the leading @ to resolveCLITarget, which populates ResolvedInvocation.CLI (the cliProviders key) and leaves BaseURL empty. Exactly one branch downstream reads it: runPreparedWithFailover in failover.go, which calls runCLIConversation instead of the hosted cascade. It is the one place a prepared conversation starts — PreparedStep.Run (every routed re-entry included) and RunHook both reach it through runAndAnnounceFailover — so both get the CLI path from that one branch.

prepareAgentStep is otherwise untouched: workspace, buildAgentTools, injectVerdictTool, context blocks, and the spill directory are all built identically. Only the HTTP client is skipped — invocationLLM returns nil for a CLI source, so the OpenRouter/repair/retry transport stack has nothing to wrap. config.cliProviders is the only table: which @name values exist (today, claude) and which binary to look for.

The bridge

The CLI is passed --tools "" unconditionally, and clibridge.go starts an MCP server in the parent process — on a loopback listener with an ephemeral port, for the duration of one attempt — that re-exports every tool in the step's registry, built-in or custom: the bridge is the entire tool surface, not a side channel next to a native one. It writes a --mcp-config document naming its URL. The config file goes to the OS temp dir, never the workspace — the workspace is captured as artifacts and readable by the agent's own file tools, and a live callback URL belongs in neither.

Always loopback, unconditionally: the CLI process is always a host subprocess of this one, whether or not the step names an image: for its tools (see infra.md), so nothing outside this host ever needs to reach it. A bridged tool's own container, if image: names one, is where the tool runs — the same toolEnv.runner a hosted agent's tools already run through — never where the CLI does.

The server is stateless (StreamableHTTPOptions{Stateless: true}). It serves exactly one child for one attempt, so per-session state would only be a way for a crashed child to strand something.

Adaptation is thin because the tool contract already fits: a toolImpl never returns a Go error, so the only translation is which result shape becomes IsError — and requiredCallSucceeded, the same predicate the HTTP loop uses, already answers that.

The bridge is also where verdicts are captured. Every successful call is inspected for the verdict/note keys, in the parent's memory, at the moment the tool runs. Nothing depends on the CLI reporting what it decided.

Enforcing required tools without tool_choice

The HTTP loop enforces required: true by forcing an unsatisfied tool through the next turn's tool_choice. That lever does not cross a process boundary. checkCLIObligations moves the check to the exit instead: unsatisfiedRequiredTools — again the same function — is consulted once the process is gone, and anything still unsatisfied is an outcome.Fail. That is strictly stronger than the hosted path's "force one more turn and hope", at the cost of not being able to nudge mid-conversation.

Tool guards (required:/max_calls:/args:) all bind on a CLI agent, because every tool reaches the bridge: max_calls: is cliBridge.overBudget, counting each call behind the same mutex max_questions: already rides; args: pins merge inside execCustomTool (or mcpToolImpl, for a pinned MCP grant, whose stripped schema the bridge re-exports as it received it), which the bridge calls unchanged; and required: is checkCLIObligations above.

One flag, not two

The child's tool surface could be two questions — --tools (which built-ins exist at all) and --allowedTools (which of those need no further prompt). It is one: --tools "" is passed unconditionally, so the CLI offers no built-in of its own regardless of the grant (verified — --tools "" reports an empty session tool list), and --allowedTools names every granted tool under the bridge's mcp__steps__* namespace, since there is no shorter native spelling left to prefer. Deny-by-default falls out of the empty value itself: a built-in this build has never heard of is withheld because --tools never grows a case for it, not because a per-grant computation remembered to exclude it.

Attestation: the init event is the fence

--tools "" is policy inside an upstream binary this build does not pin, whose built-in surface grows with releases — so the fence is enforced, not merely asked for. The CLI's stream-json init event (type: "system", subtype: "init") lists the session's tools, and cliattest.go's checkCLIToolSurface compares it against exactly the bridged grant (cliToolPermissions) — by set equality, not a subset check, so a tool the grant expected but the child never reported is caught too, and "the bridge never connected" (an empty report against a non-empty grant) is subsumed for free.

The check runs the instant parseCLIStream parses the init line, not after the stream ends, and a mismatch kills the child immediately (execCLI cancels its own derived context) before any later line — any further tool call — is even read. The absence rule has a precedence fix built in: a result event with no preceding init is an attestation failure (the child finished without ever proving its tool surface), but a stream that produced neither event stays the ordinary "exited without reporting a result" case — a child that died before saying anything was never in a position to misreport its tools either.

This is detection, not prevention — say so plainly rather than overclaiming. The kill lands after init is parsed, so a single surplus call made in the same breath as init itself could in principle land before the kill reaches the child. --tools "" remains the primary fence; attestation is the per-run proof that it held. An attestation failure is reported as an infrastructure condition (errCLIToolSurface, retry.Stop'd rather than retried) — steps refusing to trust the child is not the child's own answer to its task, and re-running it would just re-trigger the same fence.

A bridged call's trajectory entry is recorded de-namespaced (see "Reading the transcript back" below) as a second, human-readable signal alongside the machine check: a surplus native shows up as Bash in steps runs steps rather than disappearing into a mcp__steps__*-shaped record nobody reads.

Configuration scope, and what is still not hashed

--setting-sources is passed empty by default: the subprocess loads no configuration scopes — not the operator's ~/.claude (settings, hooks, plugins, skills, output styles — none of which appears in the merkle hash, so the same pipeline would behave differently per machine and a user hook could fire inside a step that never declared one), and not the repo's .claude/ either. Repo config shaping an agent is a capability, so the pipeline opts into it: settings: project on the agent entry passes --setting-sources project.

When project scope is opted in, its contents are still not hashed — but the opt-in itself is (cli_settings), so granting or revoking the scope invalidates the cache. Folding the contents in would mean reading files that may sit outside the step's declared inputs, and the honest boundary is the same one this package already draws around the CLI's version: steps hashes which thing was asked for, not the state of everything on the other side of the call. A repo's CLAUDE.md changing does not invalidate a cached CLI agent step; if that matters for a given pipeline, name the file in context_paths:, which is hashed.

Reading the transcript back

clistream.go parses the CLI's line-delimited JSON off a pipe as it arrives, rather than buffering it whole — so a step killed by its timeout still records the trajectory of what it managed to do. Parsing is tolerant by design: the event schema belongs to the CLI, so an unknown event type, an unexpected content block, or an unparsable line is logged at debug and skipped. The one intolerable case is a missing terminal result event, which distinguishes "the CLI died mid-sentence" from "the CLI finished with nothing to say" — and that distinction is what attempts: keys off.

A fork the stream does not narrate is recorded from the bridge. A prompt opening with a typed slash command (/code-review high) makes the CLI fork a subagent before init, and that fork's turns go to a sidechain file under ~/.claude, never to stdout — the stream says only system/task_started and, when it ends, system/task_notification. Every call the fork makes still crosses the bridge, so between those two events cliBridge.handler records each call and result into a child recorder one level deeper, and the parent nests it as a subagent entry named for the command when it closes — or when the stream ends, so a review aborted mid-fork keeps what it did. Two forks are left to the stream: one started by a tool call carries a tool_use_id and streams its own messages with parent_tool_use_id, and a backgrounded one runs beside the parent, so a bridged call could belong to either. Its text is the one thing still missing; only the sidechain file has it.

Tool names enter the trajectory de-namespaced: mcp__steps__read_file records as read_file, identical to what a hosted step's own trajectory shows for the same call. mcp__steps__ is our own bridge's namespace (see cliBridgeServerName), so stripping it is de-namespacing an observation, not translating one into something it wasn't — the thing that ran really was steps' read_file. A name with no such prefix (Bash, Task, anything a surplus native reports despite --tools "") is kept verbatim rather than guessed at, doubling as a second, human-readable signal of an attestation failure (see above). This is what lets one assert.tool_calls: [{name: read_file}] pass on a hosted step and a CLI step alike.

Session continuity across retries

Each CLI step mints a v4 UUID and passes --session-id on its first invocation, then --resume <same id> on every retry. Minting rather than parsing the CLI's reported session_id buys two things: the resume needs no round trip through the transcript, and cleanup can match on a name no other run could own.

The load-bearing fact, verified rather than assumed: session-scoped flags re-apply on resume. A resumed invocation re-reads --mcp-config, which is what makes this compatible with the per-attempt bridge — each attempt binds a fresh ephemeral port, and a resume that inherited the first attempt's config would be talking to a dead socket. --tools re-applies the same way. Resume also survives SIGKILL mid-run, which matters because that is the actual retry trigger.

--max-turns counts per invocation, not per session, so the cumulative budget is steps' to track: each attempt is passed max_turns - turnsSpent, and the step fails outright rather than retrying when nothing is left. Under max_turns: 0 there is no remainder to track and the flag is omitted entirely — these CLIs impose no turn cap of their own, so passing nothing IS the uncapped spelling.

Session files are deleted afterwards by globbing ~/.claude/projects/*/<session>.jsonl. Matching on the minted id rather than a derived directory name keeps the deletion precise — the only file that can match is one this step created — and the whole thing is best effort, because a step that did its work must never fail over tidying.

Hashing

AgentContentMap folds in a cli key, value-gated so every pre-existing hosted agent hashes byte-identically and no cache entry was invalidated by this shipping. Moving an agent between a CLI and a hosted provider does change its hash, which is correct — it is a different thing producing the output.

The CLI's own version is deliberately not hashed. It drifts under the operator exactly the way a hosted model's weights do, and this package has never claimed to hash the thing on the other end of the wire, only which thing was asked for. Folding in claude --version would also put a process spawn in the planning path, which steps plan and steps web run constantly.

Preflight

A CLI target's probe is exec.LookPath, nothing more. The HTTP probe sends a real request because an endpoint can be reachable and still reject the model or the key; a CLI has no equivalent cheap failure, and spawning one to find out would put a process launch in every steps web poll. A CLI that is installed but broken fails at the step with the CLI's own error, which beats anything a probe would synthesize.

The probe cache is keyed cli|<cli>|<model> rather than model|<baseURL>|<model>: "" is a perfectly ordinary BaseURL for a CLI source, so a shared key space would let a CLI agent collide with an endpoint-less hosted one.

Compacting long conversations

Every model response and every tool result is normally appended to an agent step's conversation forever, for up to max_turns turns. A long-running agent — many tool calls, large file reads — can grow past the model's real context window well before hitting that turn cap, and the provider call then errors out.

compact_after_tokens: bounds this, on an agents: entry:

agents:
- name: coder
  source: { model: openrouter/qwen/qwen3.7-flash }
  max_turns: 60
  compact_after_tokens: 40000    # smaller than the 124,160 default -- see below

jobs:
- name: build
  plan:
  - agent: coder
    messages:
      - "Do the long-horizon work."
    assert:
      stdout: Done
  assert:
    execution: [coder]
    outcome: succeeded

Once a conversation's size crosses the budget, the agent's own model is asked to summarize everything older than a recent window (roughly the most recent 30% of the budget), and the conversation continues from [summary] + [recent turns] instead of the full history. This can happen more than once in a very long conversation — each pass folds the previous summary into the new one. A summarization failure is logged and the turn proceeds uncompacted, the same failure-is-data treatment a tool failure gets — with one exception: a summary whose tokens cross a budget: (the agent's own or the job's) stops the step, below.

A compaction is visible on the run page. The step's transcript gets a compacted marker where it happened, labelled with what it did — compacted: 12 older messages summarized, last 3 kept verbatim — and the summary itself expands from it, because from that point on the summary is what the model was working from, not the turns above it. The label counts messages rather than turns (one turn can be several messages, and a previous pass's summary is one too), and names what was kept because the marker lands after every turn so far while the most recent ones were carried over verbatim. The step's head says compacted ×N, the way it says stopped early, and the recorded result carries compactions: N. A step-cache hit records neither, so a reused step shows only its skip reason.

A stall is a step warning. When the most recent turns alone outgrow the budget, summarizing again cannot help, so compaction switches off for the rest of the conversation. The step says so once, in words — warning: compaction stalled: … Raise compact_after_tokens: on this agent … — on the run page and in the terminal, badges itself compaction stalled, and records compaction_stalled: true.

The summary request is spend. It is a real model call carrying the whole older history, so the provider's reported usage for it counts toward the step's tokens, the spend table, steps runs cost, and every budget: check. A summary that crosses a ceiling stops the step, as a tool call that crosses it does: unlike a final answer, which is kept because nothing is spent after it (agents.md), a summary always has another request behind it. The breached summary is not applied and no marker is recorded, since the model never worked from it. A pipeline tuned close to its ceiling that compacts can therefore go red on a summary.

On by default, at 97% of the model's context window — unlike every other feature on this page. An agent that sets no compact_after_tokens: still gets compaction; compact_after_tokens: 0 is what disables it. This is a deliberate exception to this codebase's usual value-gating contract for opt-in features ("absent, its behavior ... byte-identical to before it existed") — merkle hashes are unaffected either way (see below), but the conversation's behavior differs for any pipeline whose agent crosses the budget.

The 3% headroom is thin on purpose: the size is the provider's own count (see below), so the headroom only has to cover tool results appended since the provider last reported. Where there is no report to use (a provider that reports none, the first turn, the turn after a compaction), the size is an estimate that misses the system prompt and tool schemas, and 3% may not be enough — set compact_after_tokens: lower for such a provider.

The window comes from the model. internal/config/agentwindow.go's contextWindows table maps a model-name fragment to that model's window, so a 1M-context model compacts at 970,000 rather than at an eighth of its capacity. A model the table does not recognize keeps the conservative 124,160 (97% of an assumed 128K) — the safe direction to be wrong in, and no behavior change for anything that was already correct.

Matching is on a normalized name — lowercased, with . folded to - — because the same model arrives spelled claude-sonnet-4-5 from Anthropic and opencode but claude-sonnet-4.5 from OpenRouter. Table fragments are therefore always written in the dashed form. Entries are ordered most-specific-first, since some families split: gpt-5.4 is ~1M but its own -mini/-nano stayed at 400K.

The numbers come from models.dev (.<provider>.models.<id>.limit.context), the catalog opencode itself resolves against. Within a family the entry is pinned to the smallest window that family is served with, so a new sibling lands low rather than high. The Anthropic block is the visible consequence of that rule: every current Claude is natively 1M, but they are enumerated individually and the claude family entry stays at 200K, so a release newer than the table is under-budgeted rather than over-budgeted.

A budget wrong by 10x is otherwise invisible — the first symptom is a stall warning that reads like a bug in the agent loop — so every agent step states the one in force:

INF agent.compaction_budget agent=coder model=google/gemini-2.5-pro compact_after_tokens=970000 context_window=1000000
INF agent.compaction_budget agent=coder model=some-local-build compact_after_tokens=124160 context_window=unknown assumed_window="128000 (set context_window: if your model differs)"

context_window: is the escape hatch, and usually the right one. No table keyed on a model name can express a host that serves a known model with a smaller window than it has natively, and none of them will have heard of a local build or a release newer than they are. Stating the window keeps the 97% arithmetic applying and makes the log line above report a derived window instead of an assumed one:

agents:
- name: reviewer
  source: { model: lmstudio/qwen2.5-coder }
  max_turns: 60
  context_window: 32000    # -> compacts at 25,600

jobs:
- name: review
  plan:
  - agent: reviewer
    messages:
      - "Review the change."
    assert:
      stdout: Reviewed
  assert:
    execution: [reviewer]
    outcome: succeeded

compact_after_tokens: still outranks it, and overrides the budget outright rather than describing the model. Prefer context_window: unless you specifically want a budget that is not 97% of the window. Neither is available on a CLI agent (@claude/...), which resolves its own window and compacts its own conversation.

Small-context and local models must set one of the two. A local build's name tells the table nothing, so it gets the 124,160 default — close to an entire typical context window, and a 32K-token local model (LM Studio, Ollama) will overflow long before that ever triggers.

The count is the provider's own, topped up by an estimate. After each response, the provider's reported prompt tokens plus completion tokens become the conversation's size: that already counts the system prompt and tool schemas, in the model's own tokenizer. Only what was appended since — the tool results about to be sent — is estimated, with a len(text)/4 heuristic. Reasoning ("thinking") tokens are left out, because they are not sent back on the next turn. Three cases fall back to estimating the whole conversation, which misses the system prompt and tool schemas: a provider that reports no usage, the first turn, and the turn right after compaction (the last report described the history that was just replaced). A fallback model mid-conversation starts from the estimate too, since it tokenizes differently. It decides when to compact; what the summary request then costs is the provider's own reported usage, counted like any other request.

Tuning one tool's inline budget

The 32,000-byte cap is the default, which is right for most tools but has no answer at either extreme — a tool whose output is mostly noise by construction (a fuzzy search whose tail costs context on every subsequent turn), or one whose output the model genuinely needs whole (a structured report that loses its meaning as a spill pointer). max_output_bytes: on a grant sets that one tool's budget in either direction:

tools:
- mcp: gopls
  tools: [go_symbol_references]
  max_output_bytes: 6000

An explicit value wins over the default, bounded above by the 10MB spill ceiling. Narrowing loses no data — overflow still spills to a file the model can read back — it only shrinks what lands inline; widening is a declared trade of context for completeness.

It is rejected on built-ins, which already carry their own output contract (read_file pages, list_dir counts entries, search_files is bounded by arithmetic); stacking a second, conflicting bound on a designed one is a bug surface rather than a knob. It is also rejected on sub-agent tools, whose result is another agent's considered answer rather than a data dump. It is valid on custom tools and on all three MCP grant forms — unlike description/required/max_calls, which stay single-tool:-only, because the tool worth capping is typically one noisy member of a tools: [...] subset and making it carry the cap in its own grant entry would open a second connection to the same server.

max_output_bytes: is part of the merkle hash (value-gated, so leaving it unset hashes byte-identically to before the field existed) — it changes what a call returns to the model, and therefore the conversation the step produces.

Compaction is lossy. Tool results are truncated to 4000 bytes apiece in the summarization prompt, and everything older than the retained window survives only as prose in the summary — not verbatim. Separately, and independently of compaction: any tool result too large to return inline (over 32,000 bytes — run_shell/a custom tool, an MCP tool's text or structured content, a sub-agent's final answer, a fix: loop's failure output) is instead saved to a file under the step's working directory, with a short <persistent_file> pointer message (path, size, a preview) taking its place in the conversation. That pointer message is well under 4000 bytes, so it survives compaction's own truncation intact — a compacted agent can still read_file whatever it had already gathered before compaction, even though the raw conversation turn that produced it is gone. read_file itself is the exception to the spill-to-file treatment: it reads up to 100,000 bytes in a single call — deliberately larger than the 32,000-byte spill threshold, so a spilled file can always be pulled back whole in one read rather than looping the model on a truncated prefix — and an oversized file read degrades to a plain truncation (spilling a file read back out to another file would be a pointless loop) with start_line/end_line to page through the rest.

Not part of the merkle hash. Like timeout:/attempts:, compact_after_tokens: is operational — it changes how a conversation manages its own context budget, not the result it's aiming for — so it never enters a step's hashed content and cannot invalidate a cached step, in either direction.

Bounding one tool call

A step's timeout: bounds the whole conversation, so a tool that hangs spends the budget every later turn was going to use — and the step dies without ever telling the model what went wrong. timeout: on a grant bounds one call of that one tool instead:

agents:
- name: watcher
  source: { model: lmstudio/qwen2.5-coder }
  tools:
  - name: await_rollout
    description: Wait for the named service to finish rolling out.
    run: |
      until grep -q done {{ .args.service | shellquote }}.log 2>/dev/null; do sleep 1; done
    timeout: 1s

jobs:
- name: watch
  plan:
  - agent: watcher
    messages:
      - "Check whether widgetd finished its rollout."
    assert:
      tool_calls:
      - name: await_rollout
      stdout: rollout status is unknown
  assert:
    execution: [watcher]
    outcome: succeeded

The polling loop above never finds its marker, so the call is cancelled at one second and the step carries on — that block is executed as written by the docs suite, deadline and all.

An expired call is data, not a dead step. The model is told on its next turn, in the same shape every other tool failure arrives in:

{"error": "await_rollout: timed out after 1s — its context was cancelled, so anything it returned may be partial: command \"until grep -q done widgetd.log...\" failed: signal: killed"}

So the agent reacts — narrows the command, tries another route, or reports what it could not learn (the example above asserts exactly that: the step succeeds, having said the rollout status is unknown). The tool's own diagnostic is kept in the message rather than replaced by it, because what was being waited on is more useful than that something was.

It explains a failure; it never declares one. Two readers act on the error key — required: satisfaction and the CLI bridge's error flag — and a result that already reads as success is left completely alone. That is what keeps the context-ignoring built-ins honest: write_file on a loaded filesystem can return past its deadline having written the whole file, and relabeling that a failure would re-force a required: tool and tell a model to redo work already on disk. (assert: tool_calls: matches on name and arguments only — it does not read success, so it cannot see a timeout either way.)

It is valid on every tool form — unlike max_output_bytes:, a deadline collides with no designed per-tool contract, and run_shell is the likeliest thing in the system to hang:

tools:
- builtin: run_shell
  timeout: 10m
- mcp: gopls
  timeout: 30s
- agent: researcher
  description: Answers a question about the codebase.
  timeout: 5m

It binds where the tool is granted. A step's tools: list selects from what its agent granted, and a selection is resolved by substituting the agent's own spec — so a deadline written there would be silently dropped, and the loader refuses it instead. The exception is an inline custom tool, which a step defines rather than selects: that spec is what runs, deadline included.

What it can actually stop is whatever honors a context: shell-backed tools, MCP calls, sub-agent conversations. The purely local built-ins (read_file, list_dir, search_files, write_file, edit_file) take no context and run to completion — an over-deadline call there is reported (and only if it failed on its own), never interrupted, because killing an impl mid-write trades a slow tool for a half-written file.

A killed shell tool does not take its own children with it. The sh -c process is signalled and its output waited on for at most two seconds (cancelWaitDelay in internal/shell), but grandchildren it spawned are not reaped — a known shortcut of the shell layer, whose complete fix is a process group per command. This matters more with a per-tool deadline than it did without one: the step no longer ends when the tool does, so a terraform apply bounded at timeout: 2m can keep writing into the same workspace the agent goes on reading for another twenty minutes. Bound work like that at the step, or make the command clean up after itself.

web_fetch carries a hard 30-second client-side cap of its own, independent of the call's context, so a timeout: there can only tighten: a longer one does not raise the ceiling, and the fetch still fails at 30 seconds.

It bounds one call, not the turn. A model can request several calls of the same tool in a single turn; they execute in sequence, each with its own fresh deadline, so timeout: 10m across eight parallel calls is still eighty minutes of the step's budget. max_calls: is what bounds the count; the step's own timeout: remains the ceiling.

Not part of the merkle hash. Like the step-level timeout:/attempts:, a tool deadline is operational — so adding or tightening one re-runs nothing. The alternative is worse than it sounds: capping a tool that hangs would invalidate every completed step of the pipeline you were trying to rescue.

Transcript persistence

Every agent step persists two records with different bounds, because they serve different readers:

The transcript is saved for every outcome — success, run failure, assert failure, capture failure — because a failed step's transcript is the one a human reconstructs from. The write happens in PreparedStep.Run before it returns, so it precedes every way the step can still fail (its assert, then the capture and node record internal/pipeline does after it). It is best-effort on a detached context, like a failed step's node record: an auxiliary record must neither mask the step's own outcome nor be dropped because the step was aborted. Re-recording the same hash replaces the row, matching nodes.

One path never writes one: RunHook/RunFix (the no-record contract — the enclosing step records the aggregate outcome). CLI-backed conversations (cli.go) write one too: parseCLIStream records the child's own text/call/result events off its stream (clistream.go), and cli.go records the system prompt and each message it sends the child directly, gated so a retry, a nudge round, or a resumed session never re-records what a prior invocation already sent.