Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(backend/copilot): upgrade claude-agent-sdk to 0.2.152 and pin the context window explicitly (#14208)

Reinier van der Leer00f0ecc34a1f
diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.py
index b80280150f..1fe5194caf 100644
--- a/autogpt_platform/backend/backend/copilot/config.py
+++ b/autogpt_platform/backend/backend/copilot/config.py
@@ -435,6 +435,17 @@ class ChatConfig(BaseSettings):
"Set to $10 to allow most tasks to complete (p50=$5.37, p75=$13.07). "
"Override via CHAT_CLAUDE_AGENT_MAX_BUDGET_USD env var.",
)
+ claude_agent_context_window: int = Field(
+ default=200_000,
+ ge=100_000,
+ le=1_000_000,
+ validation_alias=AliasChoices("CHAT_CLAUDE_AGENT_CONTEXT_WINDOW"),
+ description="Context window the SDK subprocess is held to, in tokens "
+ "(sets ``CLAUDE_CODE_AUTO_COMPACT_WINDOW``; see ``sdk/env.py``). Only "
+ "raise it on a route that really serves 1M — past the provider's real "
+ "window the compaction trigger never fires (at 1M on Moonshot it lands "
+ "at 967K against Kimi's 262,144).",
+ )
claude_agent_autocompact_pct_override: int = Field(
default=50,
ge=0,
diff --git a/autogpt_platform/backend/backend/copilot/sdk/cli_openrouter_compat_test.py b/autogpt_platform/backend/backend/copilot/sdk/cli_openrouter_compat_test.py
index e73bc89761..efd17acb1f 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/cli_openrouter_compat_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/cli_openrouter_compat_test.py
@@ -457,10 +457,12 @@ def _assert_no_forbidden_patterns(
@pytest.mark.asyncio
@pytest.mark.xfail(
- reason="CLI 2.1.97 (SDK 0.1.58) sends context-management beta without "
- "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1. This is expected — the env "
- "var guard in test_disable_experimental_betas_env_var_strips_headers "
- "is the real regression test.",
+ reason="CLI 2.1.259 (SDK 0.2.152) still sends the "
+ "context-management-2025-06-27 beta header against a non-Anthropic base "
+ "URL without CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 (observed, same as "
+ "CLI 2.1.97 / SDK 0.1.58). This is expected — the env var guard in "
+ "test_disable_experimental_betas_env_var_strips_headers is the real "
+ "regression test.",
strict=True,
)
async def test_bare_cli_does_not_send_openrouter_incompatible_features():
@@ -468,8 +470,8 @@ async def test_bare_cli_does_not_send_openrouter_incompatible_features():
Documents whether the bundled CLI sends OpenRouter-incompatible
features without the CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS env var.
- On SDK 0.1.58 (CLI 2.1.97) this is expected to fail — the env var
- test above is the actual regression guard.
+ The env var test below is the actual regression guard; this one only
+ records what the current bundled CLI does on its own.
"""
returncode, _stdout, stderr, captured = await _run_reproduction()
_assert_no_forbidden_patterns(captured, returncode, stderr)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py
index 1eefb03212..0fc2bb91fe 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py
@@ -833,8 +833,11 @@ def _serialize_messages(value: object) -> str:
if not isinstance(message, dict):
raise TypeError("Each message must be an object")
role = message.get("role")
- if role not in {"user", "assistant"}:
- raise ValueError("Message roles must be user or assistant")
+ # Anthropic accepts `system` turns inline in `messages`, not only in
+ # the top-level `system` field; the CLI uses this for mid-conversation
+ # reminders, so the role allowlist must include it.
+ if role not in {"user", "assistant", "system"}:
+ raise ValueError("Message roles must be user, assistant, or system")
normalized.append(
{
"role": role,
diff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway_test.py b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway_test.py
index 6bffdb31f0..1425ee05fa 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway_test.py
@@ -11,6 +11,7 @@ from aiohttp import ClientSession
from backend.copilot.sdk.codex_compat_gateway import (
CodexAnthropicGateway,
_safe_tool_name,
+ _serialize_messages,
)
from backend.integrations.codex.models import (
CodexDynamicToolCall,
@@ -797,3 +798,29 @@ async def test_rejects_missing_gateway_capability() -> None:
payload = await response.json()
assert response.status == 401
assert payload["error"]["type"] == "authentication_error"
+
+
+def test_serialize_messages_keeps_inline_system_turns() -> None:
+ serialized = _serialize_messages(
+ [
+ {"role": "user", "content": "hello"},
+ {
+ "role": "system",
+ "content": "<system-reminder>stay on task</system-reminder>",
+ },
+ {"role": "assistant", "content": "sure"},
+ ]
+ )
+ transcript = json.loads(serialized[serialized.index("[") :])
+
+ assert [message["role"] for message in transcript] == [
+ "user",
+ "system",
+ "assistant",
+ ]
+ assert transcript[1]["content"] == "<system-reminder>stay on task</system-reminder>"
+
+
+def test_serialize_messages_rejects_unknown_roles() -> None:
+ with pytest.raises(ValueError):
+ _serialize_messages([{"role": "tool", "content": "result"}])
diff --git a/autogpt_platform/backend/backend/copilot/sdk/env.py b/autogpt_platform/backend/backend/copilot/sdk/env.py
index 19a30c3006..968a519873 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/env.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/env.py
@@ -173,20 +173,25 @@ def build_sdk_env(
# flag harmlessly (those betas are not enabled there either by default).
env["CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS"] = "1"
- # Pin the CLI's perceived context window at the 200K default. 1M context
- # is GA (no beta header) on Sonnet 4.6+/5 and newer CLIs enable it by
- # default via their model capability table (native_1m) or server-side
- # flags; this kill-switch gates every 1M branch in the CLI's window
- # resolution, so the autocompact trigger below stays anchored to 200K
- # across SDK upgrades. (The experimental-betas flag above no longer
- # covers 1M — it stopped being a beta.)
- env["CLAUDE_CODE_DISABLE_1M_CONTEXT"] = "1"
-
- # Auto-compaction trigger threshold (CLI default: ~93% of perceived window).
- # The override caps Anthropic cache-creation cost; Moonshot routes skip it
- # because their OpenRouter endpoint returns ``cache_create=0`` (no cache
- # writes happen, so there's no cost to cap) and an aggressive trigger
- # against Kimi's larger window cascades into 3+ compactions per turn.
+ # Pin the CLI's perceived context window explicitly. This env var is the
+ # highest-precedence input to the CLI's window resolution, so it preempts
+ # the model table, the 1M capability flags and the server-side experiment
+ # branches that newer CLIs consult — without it the compaction trigger is
+ # an emergent property of whichever bundled CLI we happen to ship.
+ env["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] = str(config.claude_agent_context_window)
+
+ # The window above is clamped by the model's own window, which this
+ # kill-switch holds at 200K; keeping it set past that point would swallow
+ # the raise silently. 1M is GA (no beta header) on Sonnet 4.6+/5, so the
+ # experimental-betas flag above does not cover it.
+ if config.claude_agent_context_window <= _CLI_DEFAULT_CONTEXT_WINDOW:
+ env["CLAUDE_CODE_DISABLE_1M_CONTEXT"] = "1"
+
+ # Trigger threshold, as a percentage of the window pinned above (CLI
+ # default: ~93%). The override caps Anthropic cache-creation cost;
+ # Moonshot routes skip it because their OpenRouter endpoint returns
+ # ``cache_create=0`` (no cache writes happen, so there's no cost to cap)
+ # and an aggressive trigger cascades into 3+ compactions per turn.
# Operators can also set the config to 0 to disable globally.
if (
not is_moonshot_model(model)
@@ -207,6 +212,10 @@ def build_sdk_env(
return env
+# What the CLI assumes any Claude model's window to be once 1M is gated off.
+_CLI_DEFAULT_CONTEXT_WINDOW = 200_000
+
+
# Sonnet 5's tokenizer counts ~30% more tokens than 4.x for the same text,
# so the same 50%-of-200K trigger would compact at ~77% of the *text* budget
# 4.x sessions get. Scaling the trigger by the inflation factor keeps the
diff --git a/autogpt_platform/backend/backend/copilot/sdk/env_test.py b/autogpt_platform/backend/backend/copilot/sdk/env_test.py
index e940fe7ac4..af6d51c898 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/env_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/env_test.py
@@ -26,6 +26,10 @@ def _make_config(**overrides) -> ChatConfig:
"base_url": None,
"thinking_standard_model": "anthropic/claude-sonnet-4-6",
"thinking_advanced_model": "anthropic/claude-opus-4-7",
+ # Pinned: both are settable from the environment, and a developer's
+ # .env otherwise rewrites what build_sdk_env() is asked to emit.
+ "claude_agent_autocompact_pct_override": 50,
+ "claude_agent_context_window": 200_000,
# Aux key satisfies ``_validate_aux_client_for_direct_main`` —
# these tests target SDK behavior, not the aux check.
"aux_api_key": "or-aux-key",
@@ -599,6 +603,7 @@ class TestAutocompactPctSonnet5Scaling:
"anthropic/claude-sonnet-4-6",
"anthropic/claude-sonnet-4-5", # substring near-miss guard
"anthropic/claude-opus-4-7",
+ "anthropic/claude-opus-4-8",
],
)
def test_non_sonnet_5_not_scaled(self, model):
@@ -625,16 +630,40 @@ class TestAutocompactPctSonnet5Scaling:
class TestContextWindowPin:
- """CLAUDE_CODE_DISABLE_1M_CONTEXT pins the CLI's perceived window at
- 200K: 1M context is GA (not beta-gated) on Sonnet 4.6+/5, so the
- experimental-betas flag alone no longer prevents a future CLI from
- silently moving the autocompact trigger to ~500K."""
+ """The window the CLI compacts against is ours, not the CLI's guess.
- def test_disable_1m_context_set_in_all_modes(self):
- cfg = _make_config(use_openrouter=False)
+ At or below the default, ``CLAUDE_CODE_DISABLE_1M_CONTEXT`` holds the model
+ window itself at 200K too, making the default a real cap.
+ """
+
+ @pytest.mark.parametrize(
+ "window, expected, kill_switch",
+ [
+ # At the 200K default and a 50% override the CLI compacts at ~90K
+ # (~117K on Sonnet 5, whose override is scaled to 65%).
+ (200_000, "200000", True),
+ (200_001, "200001", False),
+ (1_000_000, "1000000", False),
+ ],
+ )
+ def test_window_pin_and_1m_kill_switch(self, window, expected, kill_switch):
+ """Above the default the kill-switch has to come off with the raise:
+ it would otherwise clamp the model window back to 200K and swallow it."""
+ cfg = _make_config(use_openrouter=False, claude_agent_context_window=window)
with patch("backend.copilot.sdk.env.config", cfg):
from backend.copilot.sdk.env import build_sdk_env
result = build_sdk_env(model="anthropic/claude-sonnet-5")
- assert result.get("CLAUDE_CODE_DISABLE_1M_CONTEXT") == "1"
+ assert result.get("CLAUDE_CODE_AUTO_COMPACT_WINDOW") == expected
+ assert ("CLAUDE_CODE_DISABLE_1M_CONTEXT" in result) is kill_switch
+
+ def test_context_window_rejects_out_of_range(self):
+ """Pydantic bounds (ge=100_000, le=1_000_000) are the only guard between
+ a typo'd env var and the CLI's own clamps."""
+ from pydantic import ValidationError
+
+ with pytest.raises(ValidationError):
+ _make_config(claude_agent_context_window=99_999)
+ with pytest.raises(ValidationError):
+ _make_config(claude_agent_context_window=1_000_001)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/sdk_compat_test.py b/autogpt_platform/backend/backend/copilot/sdk/sdk_compat_test.py
index 7cf8af3396..78961295be 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/sdk_compat_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/sdk_compat_test.py
@@ -271,6 +271,13 @@ _KNOWN_GOOD_BUNDLED_CLI_VERSIONS: frozenset[str] = frozenset(
# cache reads on every resumed SDK turn. Still requires
# CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1. Verified
# OpenRouter-safe via cli_openrouter_compat_test.py.
+ "2.1.248", # claude-agent-sdk 0.2.146 -- context-management is still
+ # an experimental beta here, still stripped by
+ # CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1.
+ "2.1.259", # claude-agent-sdk 0.2.152 -- wire shape measured identical
+ # to 2.1.248 on both the bare and the build_sdk_env-pinned
+ # path: same anthropic-beta list, same message roles, same
+ # body keys. Verified via cli_openrouter_compat_test.py.
}
)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index f12ff049f3..9d459e0dce 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -1402,11 +1402,18 @@ _COMPACTION_HEADROOM_TOKENS: int = 20_000
def _compaction_target_tokens(model: str) -> int:
"""Compaction target consistent with the CLI's autocompact threshold.
- Mirrors the bundled CLI's ``i6_()`` formula for autocompact:
+ Mirrors the bundled CLI's formula for autocompact:
``min(window * pct/100, window - 13K)``, then subtracts a 20K headroom
so post-compaction context sits comfortably below the CLI's trigger and
a follow-up assistant message doesn't immediately re-trigger.
Floors at 10K to preserve at least some history budget.
+
+ Deliberately a *different* window from the one the CLI subprocess is
+ pinned to (``ChatConfig.claude_agent_context_window``): the catalog caps
+ every Anthropic model at 200K pending the Claude-5 tokenizer soak, and
+ this path feeds our own estimate-based compressor, which needs that
+ margin. The 20K headroom absorbs the max-output reserve the CLI also
+ subtracts and this formula does not.
"""
from backend.util.prompt import DEFAULT_TOKEN_THRESHOLD, get_context_window
@@ -1414,7 +1421,7 @@ def _compaction_target_tokens(model: str) -> int:
if window is None:
return DEFAULT_TOKEN_THRESHOLD
pct = config.claude_agent_autocompact_pct_override
- cli_buffer = 13_000 # E88 in the bundled CLI
+ cli_buffer = 13_000 # the CLI's own summary buffer
if pct > 0 and not _is_moonshot_model(model):
cli_threshold = min(window * pct // 100, window - cli_buffer)
else:
@@ -2385,8 +2392,8 @@ def _build_system_prompt_value(
prompt cache. Our custom *system_prompt* is appended after the preset.
Requires CLI ≥ 2.1.98 (older CLIs crash when ``excludeDynamicSections``
- is combined with ``--resume``). The SDK bundles CLI 2.1.116 at
- ``claude-agent-sdk >= 0.1.64``, so the pin in ``pyproject.toml`` is
+ is combined with ``--resume``). The SDK bundles CLI 2.1.248 at
+ ``claude-agent-sdk >= 0.2.146``, so the pin in ``pyproject.toml`` is
the single source of truth — no external install needed.
When *cross_user_cache* is disabled, the raw *system_prompt* string is
@@ -4911,8 +4918,8 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
# ones — so all turns share the same static prefix.
#
# Requires CLI ≥ 2.1.98 (older CLIs crash when excludeDynamicSections
- # is combined with --resume). claude-agent-sdk >= 0.1.64 bundles
- # CLI 2.1.116, so the pin in pyproject.toml is sufficient — no
+ # is combined with --resume). claude-agent-sdk >= 0.2.146 bundles
+ # CLI 2.1.248, so the pin in pyproject.toml is sufficient — no
# external install or env-var override needed.
system_prompt_value = _build_system_prompt_value(
system_prompt,
diff --git a/autogpt_platform/backend/poetry.lock b/autogpt_platform/backend/poetry.lock
index a7225dabd5..30b28f1c90 100644
--- a/autogpt_platform/backend/poetry.lock
+++ b/autogpt_platform/backend/poetry.lock
@@ -983,27 +983,29 @@ files = [
[[package]]
name = "claude-agent-sdk"
-version = "0.1.64"
+version = "0.2.152"
description = "Python SDK for Claude Code"
optional = false
python-versions = ">=3.10"
groups = ["main"]
files = [
- {file = "claude_agent_sdk-0.1.64-py3-none-macosx_11_0_arm64.whl", hash = "sha256:4cf47a9e40c0a683a05afff4fac1e3d5ea7965b1e9f72a8e266c8d2efbf65904"},
- {file = "claude_agent_sdk-0.1.64-py3-none-macosx_11_0_x86_64.whl", hash = "sha256:7fe765c6482c74bc6b0b4491ad3bddd1349c25f4cdf4483191c68ea9c1336825"},
- {file = "claude_agent_sdk-0.1.64-py3-none-manylinux_2_17_aarch64.whl", hash = "sha256:605eebf46e7590e4f878572c2743954fba3f3530dfd99e10ff3b8b41a9fee757"},
- {file = "claude_agent_sdk-0.1.64-py3-none-manylinux_2_17_x86_64.whl", hash = "sha256:bbb1373ee0b4494e2db24aa10d312d22b86895b4b8f18eb5b58f99f14d827237"},
- {file = "claude_agent_sdk-0.1.64-py3-none-win_amd64.whl", hash = "sha256:453fa251e2a4aeed580c72d4c7b2cb98fc8d8d26012798126f5cb11a9829cd71"},
- {file = "claude_agent_sdk-0.1.64.tar.gz", hash = "sha256:147e513cb45095b57c37d74b8d01dd41b5f3ec7f70e408edce43a6590159c27d"},
+ {file = "claude_agent_sdk-0.2.152-py3-none-macosx_11_0_arm64.whl", hash = "sha256:6cc1e949743c7634274ae526bfd1c0a216ff9d71c6b36d94cab80f32b0d32b4c"},
+ {file = "claude_agent_sdk-0.2.152-py3-none-macosx_11_0_x86_64.whl", hash = "sha256:9c79c4ef91b15e5fae6aba62a82afb983dc360bc159902c541046227e2852c86"},
+ {file = "claude_agent_sdk-0.2.152-py3-none-manylinux_2_17_aarch64.whl", hash = "sha256:1e5b619e6fa3c98f06ddc107652f7820c4573fb73a9a24263568d9974078bbc2"},
+ {file = "claude_agent_sdk-0.2.152-py3-none-manylinux_2_17_x86_64.whl", hash = "sha256:f885bd935054082a599dcf92bbdaf3c0b853117a79375cb3aae74674a8124326"},
+ {file = "claude_agent_sdk-0.2.152-py3-none-win_amd64.whl", hash = "sha256:f422c256358997ffa3a740f9fdc8127aa8404a9d57493502dc7d2ac05ecb5486"},
+ {file = "claude_agent_sdk-0.2.152.tar.gz", hash = "sha256:0822bbdf700ccdd1acecf8365345a56bde07833afe7f8bd1bee68cba0e2fcf9a"},
]
[package.dependencies]
anyio = ">=4.0.0"
-mcp = ">=0.1.0"
+jsonschema = ">=4.20.0"
+mcp = ">=1.23.0,<3.0.0"
+sniffio = ">=1.0.0"
typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.11\""}
[package.extras]
-dev = ["anyio[trio] (>=4.0.0)", "mypy (>=1.0.0)", "pytest (>=7.0.0)", "pytest-asyncio (>=0.20.0)", "pytest-cov (>=4.0.0)", "ruff (>=0.1.0)"]
+dev = ["anyio[trio] (>=4.0.0)", "mypy (>=1.0.0)", "pytest (>=7.0.0)", "pytest-cov (>=4.0.0)", "ruff (>=0.1.0)"]
examples = ["asyncpg (>=0.27.0)", "boto3 (>=1.28.0)", "fakeredis (>=2.20.0)", "moto[s3] (>=5.0.0)", "redis (>=4.2.0)"]
otel = ["opentelemetry-api (>=1.20.0)"]
@@ -8631,4 +8633,4 @@ cffi = ["cffi (>=1.17,<2.0) ; platform_python_implementation != \"PyPy\" and pyt
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<3.14"
-content-hash = "04ea08252542d2cd874b3f3b5374b187b0af4e97b718e37e9434fe473431af51"
+content-hash = "79c0fee02fecaddc1610ffbe81c5a45170e7986b93a44e75f293af0cb7409448"
diff --git a/autogpt_platform/backend/pyproject.toml b/autogpt_platform/backend/pyproject.toml
index 8e06eea01d..54638725ac 100644
--- a/autogpt_platform/backend/pyproject.toml
+++ b/autogpt_platform/backend/pyproject.toml
@@ -19,7 +19,7 @@ autogpt-libs = { path = "../autogpt_libs", develop = true }
bcrypt = "^5.0.0"
bleach = { extras = ["css"], version = "^6.2.0" }
cachetools = "^5.5.0"
-claude-agent-sdk = "^0.1.64" # bundled CLI 2.1.116 -- 2.1.98+ fixes the --resume + excludeDynamicSections crash that used to force a per-turn 33K cache write. CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 env var strips the broken context-management beta. See sdk_compat_test.py.
+claude-agent-sdk = "^0.2.146" # 2.1.98+ fixes the --resume + excludeDynamicSections crash that used to force a per-turn 33K cache write. CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 strips the context-management beta, which OpenRouter rejects. See sdk_compat_test.py.
click = "^8.2.0"
cryptography = "^46.0"
discord-py = "^2.5.2"