From 253656b63459d225e9aef4549b77e59a6cc60a2d Mon Sep 17 00:00:00 2001 From: Debanjum Date: Tue, 27 May 2025 17:34:23 -0700 Subject: [PATCH] Fix engaging anthropic api cache for operator trajectories. It had become broken at some point due to refactoring. The cache control was getting added and removed right after in add_action_results What we actually wanted to do is clear the old cache breakpoint and put a new one at the latest operator tool result message. This should improve operator speed and lower costs with anthropic models. --- .../processor/operator/operator_agent_anthropic.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 = []