Skip to content

[Bug] Terminal continuation guard sums both attempts' usage into the client's prompt_tokens, causing premature auto-compaction (Anthropic adapter) #8877

Closed

Description

@HewJhong

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 anthropic adapter by default (server/responses/adapter-continuation.ts:182-188). terminalContinuationGuard only opts openai-chat providers 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, mergeUsage at 190-207).
  • The Chat encoder maps that summed input_tokens straight to prompt_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:

  1. 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.usage alone would also change the totals other code reads, so a separate field is needed.
  2. 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.

  1. Start OpenCodex (2.100.4 desktop build) with an anthropic provider and route anthropic/claude-haiku-5-5.
  2. Send POST /v1/chat/completions with tools set, stream: true, and a long conversation (about 200k tokens, mostly prompt-cached).
  3. 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".
  4. 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.
  5. Inspect ~/.opencodex/usage.jsonl for that requestId. It shows spend.sends: 2, empty recoveryKinds, and usage.inputTokens and usage.cacheReadInputTokens at about twice the previous request's values.
  6. 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 (OpenCode compaction.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

  1. github-actions commented on Nov 17, 2026

    @github-actions
    Contributor

    Issue reopened

    The report now contains the information required by the automated check. Thanks for updating it.

  2. added
    bug
    proxy
    tools
    on Nov 17, 2026
  3. shu-chen-dot-ca commented on Nov 17, 2026

    @shu-chen-dot-ca
    Contributor

    I'm taking this one on.

  4. HewJhong commented on Nov 17, 2026

    @HewJhong
    Author

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugproxytools

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions