shareAI-lab / shareAI-lab/learn-claude-code

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

Open Beginner friendly
#560 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
77.2k
Forks
12.4k
Avg merge
2d 5h
Merged PRs (30d)
6

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at s08_context_compact/code.py:538–539 and inspect the existing reactive-compaction retry logic and its error markers. Run the supplied offline reproduction and regression cases; done means the documented prompt is too long error triggers one compaction retry, while retry exhaustion and unrelated errors retain their existing behavior, with the three chapter READMEs synchronized.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
ai
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.