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): keep the SDK conversation's prompt cache for an hour (#15470)

Nicholas Tindle167c25aca5df
diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.py
index 2757b2167c..0821b9084e 100644
--- a/autogpt_platform/backend/backend/copilot/config.py
+++ b/autogpt_platform/backend/backend/copilot/config.py
@@ -622,6 +622,18 @@ class ChatConfig(BaseSettings):
"from the prefix. Set to True to opt in. When False, our system prompt "
"is passed as a raw string and replaces the Claude Code default prompt.",
)
+ claude_agent_prompt_cache_ttl: Literal["5m", "1h"] = Field(
+ default="1h",
+ description="Prompt-cache TTL for the main conversation on the SDK "
+ "OpenRouter path, passed to the CLI as CLAUDE_CODE_PROMPT_CACHE_TTL. "
+ "The CLI's default is 5m, so a chat resumed more than five minutes "
+ "after its last request re-sends its whole prompt as a cache write: "
+ "slower to the first token (4-6 s instead of 1-1.5 s) and billed at "
+ "the write price. 1h keeps it readable for an hour, at 2x input price "
+ "per write instead of 1.25x. The CLI applies it to main-conversation "
+ "requests, including inline helpers it counts as such; sub-agents and "
+ "other requests keep 5m. Same trade-off as `baseline_prompt_cache_ttl`.",
+ )
baseline_prompt_cache_ttl: str = Field(
default="1h",
description="TTL for the ephemeral prompt-cache markers on the baseline "
diff --git a/autogpt_platform/backend/backend/copilot/sdk/env.py b/autogpt_platform/backend/backend/copilot/sdk/env.py
index 2a5979cc30..9af6248750 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/env.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/env.py
@@ -154,6 +154,11 @@ def build_sdk_env(
if parts:
env["ANTHROPIC_CUSTOM_HEADERS"] = "\n".join(parts)
+ # The main conversation's cache TTL; sub-agents and other requests
+ # outside it keep the CLI's 5 minutes. See
+ # ``claude_agent_prompt_cache_ttl``.
+ env["CLAUDE_CODE_PROMPT_CACHE_TTL"] = config.claude_agent_prompt_cache_ttl
+
# --- Common: workspace isolation + security hardening (all modes) ---
# Route subagent temp files into the per-session workspace so output
# files are accessible (fixes /tmp/claude-0/ permission errors in E2B).
diff --git a/autogpt_platform/backend/backend/copilot/sdk/env_test.py b/autogpt_platform/backend/backend/copilot/sdk/env_test.py
index 987f9df497..181ed1f47e 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/env_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/env_test.py
@@ -96,6 +96,8 @@ class TestBuildSdkEnvDirectAnthropic:
assert "ANTHROPIC_BASE_URL" not in result
assert result.get("CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS") == "1"
assert result.get("CLAUDE_AUTOCOMPACT_PCT_OVERRIDE") == "50"
+ # Only the OpenRouter path is measured with the longer cache TTL.
+ assert "CLAUDE_CODE_PROMPT_CACHE_TTL" not in result
def test_no_anthropic_key_overrides_when_openrouter_flag_true_but_no_key(self):
"""OpenRouter flag is True but no api_key => openrouter_active is False."""
@@ -149,6 +151,29 @@ class TestBuildSdkEnvOpenRouter:
assert result.get("CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS") == "1"
assert result.get("CLAUDE_AUTOCOMPACT_PCT_OVERRIDE") == "50"
+ @pytest.mark.parametrize("ttl", ["1h", "5m"])
+ def test_main_conversation_prompt_cache_ttl(self, ttl):
+ cfg = self._openrouter_config(claude_agent_prompt_cache_ttl=ttl)
+ with patch("backend.copilot.sdk.env.config", cfg):
+ from backend.copilot.sdk.env import build_sdk_env
+
+ result = build_sdk_env()
+
+ assert result["CLAUDE_CODE_PROMPT_CACHE_TTL"] == ttl
+ # Sub-agents and other requests keep the CLI's own 5 minutes.
+ assert "CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL" not in result
+ assert "ENABLE_PROMPT_CACHING_1H" not in result
+
+ def test_prompt_cache_ttl_defaults_to_an_hour(self, monkeypatch):
+ monkeypatch.delenv("CHAT_CLAUDE_AGENT_PROMPT_CACHE_TTL", raising=False)
+ cfg = self._openrouter_config()
+ with patch("backend.copilot.sdk.env.config", cfg):
+ from backend.copilot.sdk.env import build_sdk_env
+
+ result = build_sdk_env()
+
+ assert result["CLAUDE_CODE_PROMPT_CACHE_TTL"] == "1h"
+
def test_strips_trailing_v1(self):
"""The /v1 suffix is stripped from the base URL."""
cfg = self._openrouter_config(base_url="https://openrouter.ai/api/v1")