max_tokens stop reason from Anthropic-compatible providers bypasses context-overflow recovery, making /compact unusable
To upvote this issue, give it a thumbs up. See this list for the most upvoted issues.
Please avoid AI slops, be concise, and focus on what matters for this issue.
Describe the bug
Providers using the Anthropic API format (e.g. Z.AI at https://api.z.ai/api/anthropic) return stop_reason: "max_tokens" when the input exceeds the model's context window — instead of an HTTP error. ECA handles this as a :limit-reached message which immediately stops the prompt and drops the on-finished-side-effect callback. This prevents /compact from ever completing once the context is past the limit, creating an unrecoverable state.
To Reproduce
- Configure a provider using
api: "anthropic"pointing to Z.AI (or any Anthropic-compatible endpoint that signals context overflow viamax_tokensstop reason rather than HTTP 400) - Use a model with a ~200K context limit (e.g.
glm-5.1, context limit 200000 per models.dev) - Have a long agentic session with many tool calls until context approaches/exceeds 200K tokens
- Observe
API limit reached. Tokens: {"input_tokens":202525,"output_tokens":225,...} - Run
/compact - See the same
API limit reachederror — compact never completes
Expected behavior
ECA has an existing recovery path for :context-overflow errors (errors.clj → chat.clj:1054): prune tool results then auto-compact. The max_tokens stop reason with high input tokens and low output tokens should trigger the same recovery — at minimum when a compact is in progress, the side-effect callback should not be discarded.
Specifically in anthropic.clj, the "max_tokens" case in the message_delta handler should either:
- Be treated as a context-overflow error (triggering prune + compact via the
:on-errorpath), or - The
:limit-reachedhandler inchat.cljshould preserve theon-finished-side-effectso that/compactcan still write its marker and summary even when the LLM call fails this way
Doctor
Provider: zai-coding-plan (custom, api: "anthropic", url: "https://api.z.ai/api/anthropic")
Model: zai-coding-plan/glm-5.1 (context limit 200000 from models.dev)
ECA version: 0.135.2
Additional context
The core of the issue is two disjoint code paths for the same underlying condition:
-
HTTP error path (e.g. Anthropic real API returns 400
"prompt is too long"): classified as:context-overflowbyerrors.clj→ prunes tool results → triggers auto-compact → works -
max_tokensstop reason path (Z.AI and potentially other Anthropic-compatible providers): handled as:limit-reachedmessage inchat.clj→ callsfinish-chat-prompt!with(dissoc chat-ctx :on-finished-side-effect)→ kills the compact callback, no recovery
The relevant source locations:
src/eca/llm_providers/anthropic.clj—"max_tokens"case inon-stream-fnemits:limit-reachedsrc/eca/features/chat.clj—:limit-reachedhandler inon-message-receiveddropson-finished-side-effectsrc/eca/llm_providers/errors.clj—:context-overflowclassification (only reached via:on-error)src/eca/features/chat.clj:1054— existing prune + auto-compact recovery (only reached via:on-error)
@lockie did some improvements for custom providers that use anthropic to fix that, please try latest release and LMK!
Thanks, I'll keep an eye on that!