Skip to content

Cost ​

Model calls are billed per request. There are only two sources of cost: how many requests are made, and how long each request's input is.

The second one is the easiest to overlook here: every request resends the whole history, so the cost of a session grows with the length of that history rather than with the number of turns. Keeping the history from growing without bound and keeping each request from growing are therefore two separate problems, and they are listed separately below.

Keeping each request short ​

MechanismFactWhat it saves
Prefix cachingRequest bodies are rebuilt from a whitelist: only role, content, tool_calls, tool_call_id, function_call, reasoning_content survive. Message ids and timestamps are dropped, so the prefix sent for one conversation is byte-for-byte stableWhen the provider hits the cache, the cached prefix is billed at a lower rate
Stable context identityWhen an endpoint declares providerSessionHeader (such as x-opencode-session), every request of a live agent carries the same identifier. The host uses the session id, unchanged across restarts; a delegated sub-agent gets its ownThe provider routes requests of one context together, which is what makes cache hits possible
Progressive disclosureSkill bodies, remote MCP tools and sub-agent details never enter the tool list; only entry points doDozens of MCP servers and skills leave the list length unchanged
Tool output ceilingWhen a single tool output exceeds maxToolOutput (default 32768 characters), the excess does not enter the context whole: tools such as exec write the full text to a temporary directory and the model gets a head/tail preview with the pathTens of thousands of lines of pnpm test output do not become content that every later request must resend
Delegation isolationA sub-agent has its own history and tools; the parent receives only its last message plus accumulated subAgentUsageThe dozens of tool calls inside the sub-agent never enter the parent's context
Separate helper requestsAutomatic session naming and handoff summaries go through completeOnce: not streamed, no tools, and a cheaper model can be named for them. Naming sends only the last 2000 charactersA few dozen tokens of work does not need a full-input request from the main model

Keeping the history bounded ​

MechanismFactWhat it saves
Two thresholdshandoff soft line and guard hard line, factory values 40% and 80% of the windowHanding off before the history becomes two or three times as long, instead of when it no longer fits
No request past the hard lineAbove the hard line ContextGuardPlugin returns a reason from onInnerLoopStart and the request is never sentPaying nothing for a request that cannot succeed
HandoffThe memory becomes "the template plus one short breakpoint message carrying a path"Every request afterwards carries only the template and one short message
Interruption does not redo workabortedMessageStrategy: keep keeps the partial answer and the next request continues from it; abortedToolRoundStrategy: fill keeps the whole tool round instead of redoing itInputs already paid for are not wasted by an interruption

Keeping the request count low ​

MechanismFactWhat it saves
Results come backResults of async_exec, async_timeout and MCP long tasks are delivered into the session and wake the agent from idleNo polling with sleep, and every poll is a full-input request
The queue does not lose deliveriesMessages arriving mid-run wait in the queue by priorityNo user-side retry loop of "wait until it finishes, then send again"

Observability ​

Every assistant message carries the token usage of the request that produced it (usage); when a delegation finishes, the accumulated usage of the sub-agent and its descendants is recorded on that tool result message as subAgentUsage. Sum with sumUsage(): adding up all messages in a session gives its actual consumption.

The trade-off ​

A self handoff costs 1–2 extra full-input requests; those inputs are longer than usual. What they replace is a history that keeps growing, but the cost itself is real and does not disappear because the operation is called a handoff.

The summary executor is cheap and deterministic, but it has neither the original context nor tools, so its fidelity is low. It does not automatically stand in for a failed self handoff: treating "the model failed to write the document" as "let someone else write it" would make a failed handoff look like a successful one.

MIT License