Skip to content

fix(transport): say which prompt is too long when the OS refuses to start the CLI - #1581

Open
ofri-peretz wants to merge 1 commit into
anthropics:mainfrom
ofri-peretz:fix/system-prompt-arg-limit
Open

ofri-peretz wants to merge 1 commit into
anthropics:mainfrom
ofri-peretz:fix/system-prompt-arg-limit

Conversation

@ofri-peretz

Copy link
Copy Markdown

A system_prompt string is passed to the CLI as one command-line argument. When it is too long, the OS refuses to start the CLI and the caller gets an error that names only the CLI path (#1096):

  • Linux/macOS: CLIConnectionError: Failed to start Claude Code: [Errno 7] Argument list too long, at 131,072 bytes (MAX_ARG_STRLEN).
  • Windows: CLINotFoundError: Claude Code not found at ..., which is wrong. CreateProcess caps the whole command line at 32,767 characters and raises WinError 206, which is a FileNotFoundError. That case was reported in the issue comments.

The fix is option (1) + (3) from the issue.

What changes:

  • When the spawn fails with E2BIG or WinError 206 and the longest argument is an inline prompt (--system-prompt or --append-system-prompt), connect() raises CLIConnectionError. The error names system_prompt (or its append text), the size in bytes, both platform limits, and system_prompt={"type": "file", "path": ...}, and keeps the OS error at the end.
  • The ClaudeAgentOptions.system_prompt docstring now lists the file form and the command-line limit.

What does not:

  • No prompt is rewritten or spilled to a temp file. The issue argues that would hide a real platform constraint.
  • Nothing is checked before spawning, so no threshold is guessed per platform and a prompt that would start is never rejected.
  • Any other spawn error, or an E2BIG where something else is the long argument, gives exactly the error it gave before.
  • No new exception class and no changed defaults.

Tests (tests/test_transport.py, with anyio.open_process patched to raise):

  • A 131,072-byte str prompt with E2BIG names system_prompt, the size and the file form.
  • The Windows WinError 206 case raises CLIConnectionError, not CLINotFoundError.
  • A preset append text that is too long gets the hint.
  • E2BIG with a short prompt keeps the generic message.

The first three fail on main and all four pass with this change. Locally: pytest tests/ (1,759 passed), ruff check, ruff format --check, mypy src/ scripts/.

#1097 makes a similar pre-spawn check, but it has had conflicts with main since July and no review. #1101 and #1104 spill to a temp file instead.

Fixes #1096

🤖 Generated with Claude Code

…tart the CLI

A system_prompt string (or custom prompt, or preset append text) is
passed to the CLI as one argument. Linux caps one argument at 131072
bytes (E2BIG) and Windows caps the whole command line at 32,767
characters (WinError 206), so a long prompt fails before the CLI starts.
The error named only the CLI path, and on Windows it became
CLINotFoundError ("Claude Code not found").

When the OS refuses with either error and the longest argument is an
inline prompt, raise CLIConnectionError naming system_prompt, its size,
the limits, and the {"type": "file", "path": ...} form. Document the
file form and the limit on ClaudeAgentOptions.system_prompt.

Fixes anthropics#1096

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

system_prompt over 131,071 bytes dies with 'Argument list too long' before any API request

1 participant