Repository navigation
Description
Client or integration
Other
Area
Proxy and routing
Summary
When the Anthropic terminal continuation guard re-sends a turn, the usage reported to the client is the sum of both attempts. A Chat Completions client (here OpenCode, through /v1/chat/completions) reads prompt_tokens as the current context size, so it sees roughly twice the real context and auto-compacts early.
I expected the client to receive the usage of the final attempt only, because that is the prompt the conversation continues from. The summed total is right for spend tracking, but not for the client.
How it happens (source read in 2.99.0; terminal-guard.ts is identical on dev at 2590e2e):
- The guard is enabled for the
anthropicadapter by default (server/responses/adapter-continuation.ts:182-188).terminalContinuationGuardonly optsopenai-chatproviders in, so no setting disables it for Anthropic. - On a continuation, the first attempt's usage is kept (
server/responses/terminal-guard.ts:261-269). On the final event,mergeUsage(accumulatedUsage, event.usage)sums input, cache-read and cache-write tokens of both attempts (terminal-guard.ts:280-281,mergeUsageat190-207). - The Chat encoder maps that summed
input_tokensstraight toprompt_tokens(chat/outbound.ts:47-56). - The second attempt re-sends the whole conversation plus the nudge, so a conversation of N tokens is reported as about 2N.
Suggested fix:
- Keep a separate accounting usage (summed) for spend and the request log, and send the final attempt's usage to the client encoders. Replacing
event.usagealone would also change the totals other code reads, so a separate field is needed. - Add a config switch to disable the terminal guard for the Anthropic adapter. Today it is always on there, and the nudge text (
TERMINAL_GUARD_NUDGE) is a fixed Chinese string.
Reproduction
This is observed, not deterministic, because it depends on the model ending a turn with a short announcement and no tool call.
- Start OpenCodex (2.100.4 desktop build) with an
anthropicprovider and routeanthropic/claude-haiku-5-5. - Send
POST /v1/chat/completionswithtoolsset,stream: true, and a long conversation (about 200k tokens, mostly prompt-cached). - Make the last user message contain an action word, for example "No worries, another agent will make the change and then send you an update".
- If the model replies with fewer than 280 characters, announces work ("I will check...") and makes no tool call, the guard re-sends the turn.
- Inspect
~/.opencodex/usage.jsonlfor thatrequestId. It showsspend.sends: 2, emptyrecoveryKinds, andusage.inputTokensandusage.cacheReadInputTokensat about twice the previous request's values. - The client's final usage chunk reports the same doubled
prompt_tokens.
Version
2.100.4 (the desktop build serving these requests, started 17 Nov). The code was read in 2.99.0, and src/server/responses/terminal-guard.ts is identical on dev (2590e2e). The earlier log rows (12 and 16 Nov) may come from older builds.
Operating system
macOS (Darwin)
Provider and model
anthropic, claude-haiku-5-5, reached through the Chat Completions route
Logs or error output
# ~/.opencodex/usage.jsonl, same conversation, consecutive requests (anonymised)
request A spend.sends=1 inputTokens=196436 cacheRead=195904 recoveryKinds=[] request body ~1.769 MB
request B spend.sends=2 inputTokens=396890 cacheRead=394809 recoveryKinds=[] request body ~1.783 MB
# Other doubled rows in the same long conversation (previous row -> doubled row)
175428 -> 351743
192238 -> 386713- The request body size barely changed between A and B, so the prompt did not double. Only the reported usage did.
- In one long conversation, 4 requests had
sends: 2. Three reported about 2x the previous row. Each was followed 6 to 9 seconds later by an automatic compaction at about 50% real usage (OpenCodecompaction.buffer: "15%", window 400k, so compaction at 340k). - The matching assistant replies were short (204, 271 and 218 characters), announced work, and were followed by a tool call. One contained Chinese text, consistent with the model answering the nudge.
- Across about 9,900 logged requests, 77 had
sends > 1. Many Anthropic ones have no recovery label, because the guard's continuation is sent with an undefined recovery kind (adapter-delivery.ts:176,request-log.ts:1869-1887). - I confirmed the code path by reading the source. I could not confirm from OpenCode's side that it used the doubled figure to trigger compaction. That part is inferred from timing.
Screenshots and supporting files
No response
Redacted configuration
{
"note": "No terminalContinuationGuard and no advisor setting in the OpenCodex config."
}Checks
- I searched existing issues and documentation.
- I removed secrets, tokens, account details, request credentials, and personal data.
Activity
github-actions commentedon Nov 17, 2026
Issue reopened
The report now contains the information required by the automated check. Thanks for updating it.
shu-chen-dot-ca commentedon Nov 17, 2026
I'm taking this one on.
HewJhong commentedon Nov 17, 2026
Thanks!
fix(antigravity): report final attempt usage after empty retry
fix(responses): report the final terminal-guard leg's usage to the cl…