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.
fix(backend/copilot): persist user_context prefix and guard empty Anthropic system block
Zamil Majdy54763b660b1a
diff --git a/autogpt_platform/backend/backend/blocks/llm.py b/autogpt_platform/backend/backend/blocks/llm.pyindex e76f273901..814c7c6c9a 100644--- a/autogpt_platform/backend/backend/blocks/llm.py+++ b/autogpt_platform/backend/backend/blocks/llm.py@@ -744,12 +744,12 @@ class LLMResponse(BaseModel):def convert_openai_tool_fmt_to_anthropic(openai_tools: list[dict] | None = None,-) -> Iterable[ToolParam] | anthropic.Omit:+) -> Iterable[ToolParam] | anthropic.NotGiven:"""Convert OpenAI tool format to Anthropic tool format."""if not openai_tools or len(openai_tools) == 0:- return anthropic.omit+ return anthropic.NOT_GIVENanthropic_tools = []for tool in openai_tools:@@ -999,20 +999,22 @@ async def llm_call(client = anthropic.AsyncAnthropic(api_key=credentials.api_key.get_secret_value())- resp = await client.messages.create(+ create_kwargs: dict[str, Any] = dict(model=llm_model.value,- system=[- {- "type": "text",- "text": sysprompt,- "cache_control": {"type": "ephemeral"},- }- ],messages=messages,max_tokens=max_tokens,tools=an_tools,timeout=600,)+ if sysprompt.strip():+ create_kwargs["system"] = [+ {+ "type": "text",+ "text": sysprompt,+ "cache_control": {"type": "ephemeral"},+ }+ ]+ resp = await client.messages.create(**create_kwargs)if not resp.content:raise ValueError("No content returned from Anthropic.")diff --git a/autogpt_platform/backend/backend/blocks/test/test_llm.py b/autogpt_platform/backend/backend/blocks/test/test_llm.pyindex a6fb1dd448..7bf6779a9d 100644--- a/autogpt_platform/backend/backend/blocks/test/test_llm.py+++ b/autogpt_platform/backend/backend/blocks/test/test_llm.py@@ -1111,3 +1111,181 @@ class TestExtractOpenRouterCost:def test_returns_none_for_negative_cost(self):response = self._mk_response({"x-total-cost": "-0.005"})assert llm.extract_openrouter_cost(response) is None+++class TestAnthropicCacheControl:+ """Verify that llm_call attaches cache_control to the system prompt block+ and to the last tool definition when calling the Anthropic API."""++ def _make_anthropic_credentials(self) -> llm.APIKeyCredentials:+ from pydantic import SecretStr++ return llm.APIKeyCredentials(+ id="test-anthropic-id",+ provider="anthropic",+ api_key=SecretStr("mock-anthropic-key"),+ title="Mock Anthropic key",+ expires_at=None,+ )++ @pytest.mark.asyncio+ async def test_system_prompt_sent_as_block_with_cache_control(self):+ """The system prompt is wrapped in a structured block with cache_control ephemeral."""+ mock_resp = MagicMock()+ mock_resp.content = [MagicMock(type="text", text="hello")]+ mock_resp.usage = MagicMock(input_tokens=5, output_tokens=3)++ captured_kwargs: dict = {}++ async def fake_create(**kwargs):+ captured_kwargs.update(kwargs)+ return mock_resp++ mock_client = MagicMock()+ mock_client.messages.create = fake_create++ credentials = self._make_anthropic_credentials()++ with patch("anthropic.AsyncAnthropic", return_value=mock_client):+ await llm.llm_call(+ credentials=credentials,+ llm_model=llm.LlmModel.CLAUDE_4_6_SONNET,+ prompt=[+ {"role": "system", "content": "You are an assistant."},+ {"role": "user", "content": "Hello"},+ ],+ max_tokens=100,+ )++ system_arg = captured_kwargs.get("system")+ assert isinstance(system_arg, list), "system should be a list of blocks"+ assert len(system_arg) == 1+ block = system_arg[0]+ assert block["type"] == "text"+ assert block["text"] == "You are an assistant."+ assert block.get("cache_control") == {"type": "ephemeral"}++ @pytest.mark.asyncio+ async def test_last_tool_gets_cache_control(self):+ """cache_control is placed on the last tool in the Anthropic tools list."""+ mock_resp = MagicMock()+ mock_resp.content = [MagicMock(type="text", text="ok")]+ mock_resp.usage = MagicMock(input_tokens=10, output_tokens=5)++ captured_kwargs: dict = {}++ async def fake_create(**kwargs):+ captured_kwargs.update(kwargs)+ return mock_resp++ mock_client = MagicMock()+ mock_client.messages.create = fake_create++ credentials = self._make_anthropic_credentials()+ tools = [+ {+ "type": "function",+ "function": {+ "name": "tool_a",+ "description": "First tool",+ "parameters": {"type": "object", "properties": {}, "required": []},+ },+ },+ {+ "type": "function",+ "function": {+ "name": "tool_b",+ "description": "Second tool",+ "parameters": {"type": "object", "properties": {}, "required": []},+ },+ },+ ]++ with patch("anthropic.AsyncAnthropic", return_value=mock_client):+ await llm.llm_call(+ credentials=credentials,+ llm_model=llm.LlmModel.CLAUDE_4_6_SONNET,+ prompt=[+ {"role": "system", "content": "System."},+ {"role": "user", "content": "Do something"},+ ],+ max_tokens=100,+ tools=tools,+ )++ an_tools = captured_kwargs.get("tools")+ assert isinstance(an_tools, list)+ assert len(an_tools) == 2+ assert (+ an_tools[0].get("cache_control") is None+ ), "Only last tool gets cache_control"+ assert an_tools[-1].get("cache_control") == {"type": "ephemeral"}++ @pytest.mark.asyncio+ async def test_no_tools_no_cache_control_on_tools(self):+ """When there are no tools, the Anthropic call receives anthropic.omit for tools."""+ mock_resp = MagicMock()+ mock_resp.content = [MagicMock(type="text", text="ok")]+ mock_resp.usage = MagicMock(input_tokens=5, output_tokens=2)++ captured_kwargs: dict = {}++ async def fake_create(**kwargs):+ captured_kwargs.update(kwargs)+ return mock_resp++ mock_client = MagicMock()+ mock_client.messages.create = fake_create++ credentials = self._make_anthropic_credentials()++ with patch("anthropic.AsyncAnthropic", return_value=mock_client):+ await llm.llm_call(+ credentials=credentials,+ llm_model=llm.LlmModel.CLAUDE_4_6_SONNET,+ prompt=[+ {"role": "system", "content": "System."},+ {"role": "user", "content": "Hello"},+ ],+ max_tokens=100,+ tools=None,+ )++ tools_arg = captured_kwargs.get("tools")+ assert tools_arg is llm.convert_openai_tool_fmt_to_anthropic(+ None+ ), "Empty tools should pass anthropic.NOT_GIVEN sentinel"++ @pytest.mark.asyncio+ async def test_empty_system_prompt_omits_system_key(self):+ """When sysprompt is empty, the 'system' key must not be sent to Anthropic.++ Anthropic rejects empty text blocks; the guard in llm_call must ensure+ the system argument is omitted entirely when no system messages are present.+ """+ mock_resp = MagicMock()+ mock_resp.content = [MagicMock(type="text", text="ok")]+ mock_resp.usage = MagicMock(input_tokens=3, output_tokens=2)++ captured_kwargs: dict = {}++ async def fake_create(**kwargs):+ captured_kwargs.update(kwargs)+ return mock_resp++ mock_client = MagicMock()+ mock_client.messages.create = fake_create++ credentials = self._make_anthropic_credentials()++ with patch("anthropic.AsyncAnthropic", return_value=mock_client):+ await llm.llm_call(+ credentials=credentials,+ llm_model=llm.LlmModel.CLAUDE_4_6_SONNET,+ prompt=[{"role": "user", "content": "Hi"}],+ max_tokens=50,+ )++ assert (+ "system" not in captured_kwargs+ ), "system must be omitted when sysprompt is empty to avoid Anthropic 400"diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex 7f47fa66da..90d41b2225 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -984,15 +984,8 @@ async def stream_chat_completion_baseline(else:base_system_prompt, understanding = await prompt_task- # Append user message to transcript.- # Always append when the message is present and is from the user,- # even on duplicate-suppressed retries (is_new_message=False).- # The loaded transcript may be stale (uploaded before the previous- # attempt stored this message), so skipping it would leave the- # transcript without the user turn, creating a malformed- # assistant-after-assistant structure when the LLM reply is added.- if message and is_user_message:- transcript_builder.append_user(content=message)+ # Append user message to transcript after context injection below so the+ # transcript receives the prefixed message when user context is available.# Generate title for new sessionsif is_user_message and not session.title:@@ -1045,19 +1038,41 @@ async def stream_chat_completion_baseline(# Inject user context into the first user message on first turn.# Done before attachment/URL injection so the context prefix lands at# the very start of the message content.+ # The prefixed content is also stored back into session.messages and the+ # transcript so that resumed sessions and the transcript both carry the+ # personalisation beyond the first request.+ user_message_for_transcript = messageif should_inject_user_context and understanding:user_ctx = format_understanding_for_prompt(understanding)- injected = False+ prefixed: str | None = Nonefor msg in openai_messages:if msg["role"] == "user":- msg["content"] = (+ prefixed = (f"<user_context>\n{user_ctx}\n</user_context>\n\n{msg['content']}")- injected = True+ msg["content"] = prefixedbreak- if not injected:+ if prefixed is not None:+ # Persist the prefixed content so subsequent turns and --resume+ # retain the user context.+ for session_msg in session.messages:+ if session_msg.role == "user":+ session_msg.content = prefixed+ break+ user_message_for_transcript = prefixed+ else:logger.warning("[Baseline] No user message found for context injection")+ # Append user message to transcript.+ # Always append when the message is present and is from the user,+ # even on duplicate-suppressed retries (is_new_message=False).+ # The loaded transcript may be stale (uploaded before the previous+ # attempt stored this message), so skipping it would leave the+ # transcript without the user turn, creating a malformed+ # assistant-after-assistant structure when the LLM reply is added.+ if message and is_user_message:+ transcript_builder.append_user(content=user_message_for_transcript or message)+# --- File attachments (feature parity with SDK path) ---working_dir: str | None = Noneattachment_hint = ""diff --git a/autogpt_platform/backend/backend/copilot/prompt_cache_test.py b/autogpt_platform/backend/backend/copilot/prompt_cache_test.pynew file mode 100644index 0000000000..7bec927cb5--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/prompt_cache_test.py@@ -0,0 +1,146 @@+"""Unit tests for the cacheable system prompt building logic.++These tests verify that _build_cacheable_system_prompt:+- Returns the static _CACHEABLE_SYSTEM_PROMPT when no user_id is given+- Returns the static prompt + understanding when user_id is given+- Falls through to _CACHEABLE_SYSTEM_PROMPT when Langfuse is not configured+- Returns the Langfuse-compiled prompt when Langfuse is configured+- Handles DB errors and Langfuse errors gracefully+"""++from unittest.mock import AsyncMock, MagicMock, patch++import pytest++_SVC = "backend.copilot.service"+++class TestBuildCacheableSystemPrompt:+ @pytest.mark.asyncio+ async def test_no_user_id_returns_static_prompt(self):+ """When user_id is None, no DB lookup happens and the static prompt is returned."""+ with (patch(f"{_SVC}._is_langfuse_configured", return_value=False),):+ from backend.copilot.service import (+ _CACHEABLE_SYSTEM_PROMPT,+ _build_cacheable_system_prompt,+ )++ prompt, understanding = await _build_cacheable_system_prompt(None)++ assert prompt == _CACHEABLE_SYSTEM_PROMPT+ assert understanding is None++ @pytest.mark.asyncio+ async def test_with_user_id_fetches_understanding(self):+ """When user_id is provided, understanding is fetched and returned alongside prompt."""+ fake_understanding = MagicMock()+ mock_db = MagicMock()+ mock_db.get_business_understanding = AsyncMock(return_value=fake_understanding)++ with (+ patch(f"{_SVC}._is_langfuse_configured", return_value=False),+ patch(f"{_SVC}.understanding_db", return_value=mock_db),+ ):+ from backend.copilot.service import (+ _CACHEABLE_SYSTEM_PROMPT,+ _build_cacheable_system_prompt,+ )++ prompt, understanding = await _build_cacheable_system_prompt("user-123")++ assert prompt == _CACHEABLE_SYSTEM_PROMPT+ assert understanding is fake_understanding+ mock_db.get_business_understanding.assert_called_once_with("user-123")++ @pytest.mark.asyncio+ async def test_db_error_returns_prompt_with_no_understanding(self):+ """When the DB raises an exception, understanding is None and prompt is still returned."""+ mock_db = MagicMock()+ mock_db.get_business_understanding = AsyncMock(+ side_effect=RuntimeError("db down")+ )++ with (+ patch(f"{_SVC}._is_langfuse_configured", return_value=False),+ patch(f"{_SVC}.understanding_db", return_value=mock_db),+ ):+ from backend.copilot.service import (+ _CACHEABLE_SYSTEM_PROMPT,+ _build_cacheable_system_prompt,+ )++ prompt, understanding = await _build_cacheable_system_prompt("user-456")++ assert prompt == _CACHEABLE_SYSTEM_PROMPT+ assert understanding is None++ @pytest.mark.asyncio+ async def test_langfuse_compiled_prompt_returned(self):+ """When Langfuse is configured and returns a prompt, the compiled text is returned."""+ fake_understanding = MagicMock()+ mock_db = MagicMock()+ mock_db.get_business_understanding = AsyncMock(return_value=fake_understanding)++ langfuse_prompt_text = "You are a Langfuse-sourced assistant."+ mock_prompt_obj = MagicMock()+ mock_prompt_obj.compile.return_value = langfuse_prompt_text++ mock_langfuse = MagicMock()+ mock_langfuse.get_prompt.return_value = mock_prompt_obj++ with (+ patch(f"{_SVC}._is_langfuse_configured", return_value=True),+ patch(f"{_SVC}.understanding_db", return_value=mock_db),+ patch(f"{_SVC}._get_langfuse", return_value=mock_langfuse),+ patch(+ f"{_SVC}.asyncio.to_thread", new=AsyncMock(return_value=mock_prompt_obj)+ ),+ ):+ from backend.copilot.service import _build_cacheable_system_prompt++ prompt, understanding = await _build_cacheable_system_prompt("user-789")++ assert prompt == langfuse_prompt_text+ assert understanding is fake_understanding+ mock_prompt_obj.compile.assert_called_once_with(users_information="")++ @pytest.mark.asyncio+ async def test_langfuse_error_falls_back_to_static_prompt(self):+ """When Langfuse raises an error, the fallback _CACHEABLE_SYSTEM_PROMPT is used."""+ mock_db = MagicMock()+ mock_db.get_business_understanding = AsyncMock(return_value=None)++ with (+ patch(f"{_SVC}._is_langfuse_configured", return_value=True),+ patch(f"{_SVC}.understanding_db", return_value=mock_db),+ patch(+ f"{_SVC}.asyncio.to_thread",+ new=AsyncMock(side_effect=RuntimeError("langfuse down")),+ ),+ ):+ from backend.copilot.service import (+ _CACHEABLE_SYSTEM_PROMPT,+ _build_cacheable_system_prompt,+ )++ prompt, understanding = await _build_cacheable_system_prompt("user-000")++ assert prompt == _CACHEABLE_SYSTEM_PROMPT+ assert understanding is None+++class TestCacheableSystemPromptContent:+ """Smoke-test the _CACHEABLE_SYSTEM_PROMPT constant for key structural requirements."""++ def test_cacheable_prompt_has_no_placeholder(self):+ """The static cacheable prompt must not contain format placeholders."""+ from backend.copilot.service import _CACHEABLE_SYSTEM_PROMPT++ assert "{users_information}" not in _CACHEABLE_SYSTEM_PROMPT+ assert "{" not in _CACHEABLE_SYSTEM_PROMPT++ def test_cacheable_prompt_mentions_user_context(self):+ """The prompt instructs the model to parse <user_context> blocks."""+ from backend.copilot.service import _CACHEABLE_SYSTEM_PROMPT++ assert "user_context" in _CACHEABLE_SYSTEM_PROMPTdiff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 9e20d65572..407137f2e1 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -2031,11 +2031,21 @@ async def stream_chat_completion_sdk(# On the first turn inject user context into the message instead of the# system prompt — the system prompt is now static (same for all users)# so the LLM can cache it across sessions.+ # current_message is updated so the transcript and session.messages also+ # store the prefixed content, preserving personalisation across turns and+ # on --resume.if not has_history and understanding:user_ctx = format_understanding_for_prompt(understanding)- query_message = (- f"<user_context>\n{user_ctx}\n</user_context>\n\n{query_message}"+ prefixed_message = (+ f"<user_context>\n{user_ctx}\n</user_context>\n\n{current_message}")+ current_message = prefixed_message+ query_message = prefixed_message+ # Persist the prefixed content so resumed sessions retain the context.+ for session_msg in session.messages:+ if session_msg.role == "user":+ session_msg.content = prefixed_message+ break# If files are attached, prepare them: images become vision# content blocks in the user message, other files go to sdk_cwd.attachments = await _prepare_file_attachments(diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 3f013c4509..17c87dbc1d 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -9210,7 +9210,14 @@},"CreditTransactionType": {"type": "string",- "enum": ["TOP_UP", "USAGE", "GRANT", "REFUND", "CARD_CHECK"],+ "enum": [+ "TOP_UP",+ "USAGE",+ "GRANT",+ "REFUND",+ "CARD_CHECK",+ "SUBSCRIPTION"+ ],"title": "CreditTransactionType"},"DeleteFileResponse": {