eca icon indicating copy to clipboard operation
eca copied to clipboard

max_tokens stop reason from Anthropic-compatible providers bypasses context-overflow recovery, making /compact unusable

Open lockie opened this issue 6 months ago • 2 comments

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

  1. Configure a provider using api: "anthropic" pointing to Z.AI (or any Anthropic-compatible endpoint that signals context overflow via max_tokens stop reason rather than HTTP 400)
  2. Use a model with a ~200K context limit (e.g. glm-5.1, context limit 200000 per models.dev)
  3. Have a long agentic session with many tool calls until context approaches/exceeds 200K tokens
  4. Observe API limit reached. Tokens: {"input_tokens":202525,"output_tokens":225,...}
  5. Run /compact
  6. See the same API limit reached error — 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-error path), or
  • The :limit-reached handler in chat.clj should preserve the on-finished-side-effect so that /compact can 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:

  1. HTTP error path (e.g. Anthropic real API returns 400 "prompt is too long"): classified as :context-overflow by errors.clj → prunes tool results → triggers auto-compact → works

  2. max_tokens stop reason path (Z.AI and potentially other Anthropic-compatible providers): handled as :limit-reached message in chat.clj → calls finish-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 in on-stream-fn emits :limit-reached
  • src/eca/features/chat.clj — :limit-reached handler in on-message-received drops on-finished-side-effect
  • src/eca/llm_providers/errors.clj — :context-overflow classification (only reached via :on-error)
  • src/eca/features/chat.clj:1054 — existing prune + auto-compact recovery (only reached via :on-error)

lockie avatar May 27 '26 14:05 lockie

@lockie did some improvements for custom providers that use anthropic to fix that, please try latest release and LMK!

ericdallo avatar May 27 '26 18:05 ericdallo

Thanks, I'll keep an eye on that!

lockie avatar May 27 '26 23:05 lockie