[s08] Reactive compaction does not recognize Anthropic's "prompt is too long" error

Author: zmylolCreated Sep 6, 2026Updated Sep 6, 2026

S08 does not enter reactive compaction when Anthropic rejects an oversized prompt with its documented error message, prompt is too long. Instead, agent_loop() propagates the error immediately.

The matcher at s08_context_compact/code.py:538–539 only recognizes prompt_too_long and too many tokens. Anthropic documents the input-overflow response as a 400 invalid_request_error with the message prompt is too long: official documentation.

Reproduction

Reproduced on upstream main commit 0dcafa2ae053a1ddd6a72f265431104b08a5aa13, Python 3.13.11, macOS. The script below uses the real SDK exception class and mocked API responses, so it makes no model requests.

After installing the project's Python dependencies, save this as repro.py and run python repro.py /absolute/path/to/checkout:

"""Offline reproduction; usage: python3.13 SCRIPT /path/to/repository"""
import importlib.util
import os
from pathlib import Path
import sys
import tempfile
from types import SimpleNamespace

import anthropic
import httpx

sys.dont_write_bytecode = True
repo = Path(sys.argv[1]).resolve()
os.environ["MODEL_ID"] = "offline-model"
os.environ["ANTHROPIC_API_KEY"] = "offline-test-key"

with tempfile.TemporaryDirectory() as temp:
    os.chdir(temp)
    spec = importlib.util.spec_from_file_location("s08_repro", repo / "s08_context_compact/code.py")
    lesson = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(lesson)

    events = []
    error_message = "prompt is too long: 210445 tokens > 200000 maximum"
    error = anthropic.BadRequestError(
        error_message,
        response=httpx.Response(400, request=httpx.Request("POST", "https://api.anthropic.com/v1/messages")),
        body={"error": {"type": "invalid_request_error", "message": error_message}},
    )

    def fake_create(**kwargs):
        events.append("API")
        if events.count("API") == 1:
            raise error
        return SimpleNamespace(content=[SimpleNamespace(type="text", text="Recovered")], stop_reason="end_turn")

    def fake_reactive_compact(messages, active_request):
        events.append("reactive_compact")
        return messages

    lesson.client.messages.create = fake_create
    lesson.COMPACTOR.reactive_compact = fake_reactive_compact
    try:
        lesson.agent_loop([{"role": "user", "content": "continue"}], "continue")
    except anthropic.BadRequestError as exc:
        print("Unhandled:", type(exc).__name__, str(exc))
    print("Actual events:", events)
    print("Expected events:", ["API", "reactive_compact", "API"])

Actual output:

Unhandled: BadRequestError prompt is too long: 210445 tokens > 200000 maximum
Actual events: ['API']
Expected events: ['API', 'reactive_compact', 'API']

Expected behavior

When the API returns this input-overflow error, run reactive compaction and retry once, as described in the lesson. Preserve the existing retry limit and propagation of unrelated errors.

Suggested minimal fix

Recognize prompt is too long alongside the two existing markers, and synchronize the three chapter READMEs. I have verified this small fix locally with regression cases for recovery, existing marker compatibility, retry exhaustion, and unrelated errors.

Prepared with AI assistance. The reproduction was executed locally without live model calls.

Source: shareAI-lab/learn-claude-code