diff --git a/src/khoj/processor/operator/operator_agent_anthropic.py b/src/khoj/processor/operator/operator_agent_anthropic.py index 4256ff74..f8837c2f 100644 --- a/src/khoj/processor/operator/operator_agent_anthropic.py +++ b/src/khoj/processor/operator/operator_agent_anthropic.py @@ -206,11 +206,6 @@ class AnthropicOperatorAgent(OperatorAgent): if env_step.error: action_result["is_error"] = True - # Append tool results to the message history - self.messages += [AgentMessage(role="environment", content=agent_action.action_results)] - - # Mark the final tool result as a cache break point - agent_action.action_results[-1]["cache_control"] = {"type": "ephemeral"} # Remove previous cache controls for msg in self.messages: if msg.role == "environment" and isinstance(msg.content, list): @@ -218,6 +213,12 @@ class AnthropicOperatorAgent(OperatorAgent): if isinstance(block, dict) and "cache_control" in block: del block["cache_control"] + # Mark the final tool result as a cache break point + agent_action.action_results[-1]["cache_control"] = {"type": "ephemeral"} + + # Append tool results to the message history + self.messages += [AgentMessage(role="environment", content=agent_action.action_results)] + def _format_message_for_api(self, messages: list[AgentMessage]) -> list[dict]: """Format Anthropic response into a single string.""" formatted_messages = []