Skip to content

feat(types): widen SdkBeta to accept arbitrary anthropic-beta values#877

Open
abhicris wants to merge 1 commit intoanthropics:mainfrom
abhicris:feat/widen-sdkbeta-arbitrary-headers
Open

feat(types): widen SdkBeta to accept arbitrary anthropic-beta values#877
abhicris wants to merge 1 commit intoanthropics:mainfrom
abhicris:feat/widen-sdkbeta-arbitrary-headers

Conversation

@abhicris
Copy link
Copy Markdown

Closes #845.

What

SdkBeta is currently Literal["context-1m-2025-08-07"], so type checkers reject any other beta flag — even though the runtime path in subprocess_cli.py:

if self._options.betas:
    cmd.extend([\"--betas\", \",\".join(self._options.betas)])

is already happy to forward arbitrary strings. That blocks callers from opting into header-only betas like token-efficient-tools-2025-02-19, which on tool-heavy agent loops materially reduces output tokens (~14% on the workloads the issue cites).

Why this approach

The issue (#845) raises two options. This PR takes option 1 (loosen the literal) because it is purely additive — no existing usage breaks, no runtime behavior changes — and the Literal arm keeps documenting the value the SDK explicitly supports today. Option 2 (extra_headers) is a larger surface change and worth keeping for a follow-up if maintainers want a separate channel for non-anthropic-beta headers.

Diff shape

  • src/claude_agent_sdk/types.py: SdkBeta = Literal[\"context-1m-2025-08-07\"] | str plus a couple of comment lines explaining the intent. Updated the betas field docstring on ClaudeAgentOptions to spell out that arbitrary header values are accepted, with token-efficient-tools-2025-02-19 as the canonical example.
  • tests/test_transport.py: two new _build_command tests:
    • test_build_command_forwards_known_beta — guards the existing literal value end-to-end.
    • test_build_command_forwards_arbitrary_beta — passes a known + unknown beta side-by-side and asserts they reach --betas in order, joined as the CLI expects.

+51 / -2. The widened alias compiles to the same wider str at runtime, so type checkers will see betas: list[str]. The Literal member remains useful for IDE autocomplete on the value the SDK officially documents.

Test plan

  • ruff check and ruff format --check clean on the touched files.
  • pytest tests/test_transport.py tests/test_types.py — 130 passed (the 2 unrelated failures both also fail on main for me, they need a trio backend not relevant to this change).
  • Manual: passed betas=[\"context-1m-2025-08-07\", \"token-efficient-tools-2025-02-19\", \"some-future-beta-2099-12-31\"] through ClaudeAgentOptions constructor — runtime accepts; new test asserts the comma-joined value reaches --betas exactly.

Notes for reviewers

Closes anthropics#845.

`SdkBeta` was `Literal["context-1m-2025-08-07"]`, so any other beta
flag was rejected by type checkers even though the runtime path
(`subprocess_cli.py: cmd.extend(["--betas", ",".join(self._options.betas)])`)
was already happy to forward arbitrary strings. That blocked callers
from opting into header-only betas like
`token-efficient-tools-2025-02-19`, which on tool-heavy agent loops
materially reduces output tokens.

Widen the alias to `Literal["context-1m-2025-08-07"] | str` — known
values still autocomplete in editors, anything else passes through.
This is purely additive: no existing usage breaks, no runtime
behavior changes, and the `Literal` arm keeps documenting the value
the SDK explicitly supports today.

Update the `betas` docstring to reflect the relaxed contract and to
point out `token-efficient-tools-2025-02-19` as a representative
example.

Add two `_build_command` regression tests in `tests/test_transport.py`:

- `test_build_command_forwards_known_beta` — guards the existing
  literal value end-to-end.
- `test_build_command_forwards_arbitrary_beta` — passes a known +
  unknown beta side-by-side and asserts they reach `--betas` in
  order, joined as the CLI expects.

`+51 / -2`. `ruff check` and `ruff format --check` clean.
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.

[FEATURE] Allow opting into additional anthropic-beta headers (e.g. token-efficient-tools-2025-02-19)

1 participant