Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(ccr): skip CCR when model calls headroom_retrieve alongside user tools (#839)

Ashish30078f8465fb
diff --git a/headroom/ccr/response_handler.py b/headroom/ccr/response_handler.py
index 8f9f5dc03..00e33ce16 100644
--- a/headroom/ccr/response_handler.py
+++ b/headroom/ccr/response_handler.py
@@ -432,6 +432,19 @@ class CCRResponseHandler:
# No CCR tool calls, we're done
break
+ # If the model called CCR alongside non-CCR tools, we cannot build
+ # a valid continuation — every tool_use in the assistant message
+ # requires a matching tool_result, but we only have CCR results.
+ # Skip CCR handling and let the client resolve all tool calls.
+ if other_calls:
+ logger.warning(
+ "CCR: Skipping CCR handling — model called %d non-CCR tool(s) "
+ "alongside headroom_retrieve. Cannot create a valid continuation "
+ "without results for the other tools. Client must handle all tool calls.",
+ len(other_calls),
+ )
+ break
+
rounds += 1
with self._retrieval_count_lock:
self._retrieval_count += len(ccr_calls)
diff --git a/tests/test_ccr_response_handler.py b/tests/test_ccr_response_handler.py
index 5897ae63a..d51cc0949 100644
--- a/tests/test_ccr_response_handler.py
+++ b/tests/test_ccr_response_handler.py
@@ -478,6 +478,52 @@ class TestCCRResponseHandling:
assert result == response
+ @pytest.mark.asyncio
+ async def test_handle_response_mixed_tools_skips_ccr(self):
+ """When CCR and non-CCR tools are called together, skip CCR.
+
+ Building a valid continuation is impossible without results for the
+ non-CCR tools (Anthropic requires every tool_use to have a
+ tool_result). Skipping CCR avoids a wasted 400 API call and returns
+ the original response immediately so the client can resolve all
+ tool calls itself.
+ """
+ store = get_compression_store()
+ hash_key = store.store(original="[1,2,3]", compressed="[]")
+
+ handler = CCRResponseHandler()
+
+ mixed_response = {
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "ccr_call",
+ "name": CCR_TOOL_NAME,
+ "input": {"hash": hash_key},
+ },
+ {
+ "type": "tool_use",
+ "id": "user_call",
+ "name": "read_file",
+ "input": {"path": "/etc/config"},
+ },
+ ]
+ }
+
+ api_call_count = 0
+
+ async def mock_api_call(messages, tools):
+ nonlocal api_call_count
+ api_call_count += 1
+ return {"content": [{"type": "text", "text": "continuation"}]}
+
+ result = await handler.handle_response(mixed_response, [], None, mock_api_call, "anthropic")
+
+ # CCR skipped — no continuation call made (avoids the 400 API round-trip)
+ assert api_call_count == 0, "should not attempt continuation with mixed tools"
+ # Original response returned unchanged so client can handle all tool calls
+ assert result is mixed_response
+
class TestCCRResponseHandlerStats:
"""Test handler statistics."""