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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
Fix mypy errors and add network timeout handler for flaky CI tests
chopratejas52da66297978
diff --git a/headroom/compression/universal.py b/headroom/compression/universal.pyindex 2025b25bc..73203b2b9 100644--- a/headroom/compression/universal.py+++ b/headroom/compression/universal.py@@ -375,7 +375,7 @@ class UniversalCompressor:original_tokens=self._estimate_tokens(original),compressed_tokens=self._estimate_tokens(compressed),)- return key+ return str(key) if key else Noneexcept ImportError:logger.debug("CCR store not available")return Nonediff --git a/headroom/evals/memory/runner_v2.py b/headroom/evals/memory/runner_v2.pyindex 31fe715a9..a25a72b80 100644--- a/headroom/evals/memory/runner_v2.py+++ b/headroom/evals/memory/runner_v2.py@@ -403,8 +403,8 @@ class LoCoMoEvaluatorV2:finally:# Clean up backend- if self._backend:- await self._backend.close()+ if self._backend and hasattr(self._backend, "close"):+ await self._backend.close() # type: ignore[union-attr]# Calculate final metricsduration = time.time() - start_timediff --git a/headroom/evals/memory/runner_v3.py b/headroom/evals/memory/runner_v3.pyindex 982687a63..c066b5a06 100644--- a/headroom/evals/memory/runner_v3.py+++ b/headroom/evals/memory/runner_v3.py@@ -277,8 +277,8 @@ class LoCoMoEvaluatorV3:all_results.append(result)finally:- if self._backend:- await self._backend.close()+ if self._backend and hasattr(self._backend, "close"):+ await self._backend.close() # type: ignore[union-attr]# Aggregate resultsduration = time.time() - start_timediff --git a/headroom/image/compressor.py b/headroom/image/compressor.pyindex 5f1c9f0a9..9e4fa590a 100644--- a/headroom/image/compressor.py+++ b/headroom/image/compressor.py@@ -241,7 +241,7 @@ class ImageCompressor:# High detail: 85 tokens per 512x512 tile + 170 basetiles_x = (width + 511) // 512tiles_y = (height + 511) // 512- return 85 * tiles_x * tiles_y + 170+ return int(85 * tiles_x * tiles_y + 170)def _apply_compression(self,diff --git a/headroom/integrations/langchain/agents.py b/headroom/integrations/langchain/agents.pyindex 409e5999f..686e358a2 100644--- a/headroom/integrations/langchain/agents.py+++ b/headroom/integrations/langchain/agents.py@@ -207,7 +207,7 @@ class HeadroomToolWrapper:# Check if compression is neededif len(result) < self.min_chars_to_compress:self._record_metrics(result, result, was_compressed=False)- return result+ return str(result)# Try to compresscompressed = self._compress_output(result)diff --git a/headroom/memory/adapters/sqlite.py b/headroom/memory/adapters/sqlite.pyindex 3442a699a..073538498 100644--- a/headroom/memory/adapters/sqlite.py+++ b/headroom/memory/adapters/sqlite.py@@ -143,7 +143,7 @@ class SQLiteMemoryStore:"""Serialize numpy array to bytes for BLOB storage."""if embedding is None:return None- return embedding.astype(np.float32).tobytes()+ return bytes(embedding.astype(np.float32).tobytes())def _deserialize_embedding(self, data: bytes | None, dim: int | None = Nonediff --git a/headroom/memory/backends/direct_mem0.py b/headroom/memory/backends/direct_mem0.pyindex 248ae9783..4c20b1537 100644--- a/headroom/memory/backends/direct_mem0.py+++ b/headroom/memory/backends/direct_mem0.py@@ -232,7 +232,7 @@ class DirectMem0Adapter:input=text,model=self._config.embedder_model,)- return response.data[0].embedding+ return list(response.data[0].embedding)def _generate_id(self, content: str, user_id: str) -> str:"""Generate a deterministic ID for a memory."""diff --git a/headroom/pricing/litellm_pricing.py b/headroom/pricing/litellm_pricing.pyindex 0ab536e31..f8d95f16c 100644--- a/headroom/pricing/litellm_pricing.py+++ b/headroom/pricing/litellm_pricing.py@@ -37,7 +37,7 @@ def get_litellm_model_cost() -> dict[str, Any]:Returns:Dictionary mapping model names to their pricing/capability info."""- return litellm.model_cost+ return litellm.model_cost # type: ignore[no-any-return]def get_model_pricing(model: str) -> LiteLLMModelPricing | None:diff --git a/headroom/providers/anthropic.py b/headroom/providers/anthropic.pyindex 6da9f4f53..85df9961d 100644--- a/headroom/providers/anthropic.py+++ b/headroom/providers/anthropic.py@@ -579,7 +579,7 @@ class AnthropicProvider(Provider):"cached_input", pricing["input"])cost += cached_cost- return cost+ return cost # type: ignore[no-any-return]except Exception as e:logger.debug(f"LiteLLM cost estimation failed for {model}: {e}")@@ -596,7 +596,7 @@ class AnthropicProvider(Provider):+ (output_tokens / 1_000_000) * pricing["output"])- return cost+ return cost # type: ignore[no-any-return]def _get_pricing(self, model: str) -> dict[str, float] | None:"""Get pricing for a model with fallback logic."""diff --git a/headroom/providers/cohere.py b/headroom/providers/cohere.pyindex 043efa5a3..2b205a0a2 100644--- a/headroom/providers/cohere.py+++ b/headroom/providers/cohere.py@@ -271,11 +271,11 @@ class CohereProvider(Provider):if info and "max_input_tokens" in info:result = info["max_input_tokens"]if result is not None:- return result+ return int(result)if info and "max_tokens" in info:result = info["max_tokens"]if result is not None:- return result+ return int(result)except Exception:pass@@ -335,7 +335,7 @@ class CohereProvider(Provider):completion_tokens=output_tokens,)if cost is not None:- return cost+ return float(cost)except Exception:passdiff --git a/headroom/proxy/server.py b/headroom/proxy/server.pyindex dacec795f..33aa53d65 100644--- a/headroom/proxy/server.py+++ b/headroom/proxy/server.py@@ -4035,7 +4035,7 @@ class HeadroomProxy:try:response = await self.http_client.get(url, headers=headers) # type: ignore[union-attr]if response.status_code == 200:- return response.text+ return str(response.text)logger.error(f"Failed to download file {file_id}: {response.status_code}")return Noneexcept Exception as e:diff --git a/tests/test_memory/test_core_operations.py b/tests/test_memory/test_core_operations.pyindex cff3eae21..9eff5dbe2 100644--- a/tests/test_memory/test_core_operations.py+++ b/tests/test_memory/test_core_operations.py@@ -9,6 +9,9 @@ Tests cover:- History chain traversal- Deletion operations- Convenience methods (remember, recall, get_user_memories, get_session_memories)++Note: These are integration tests that may hit external embedding APIs.+Tests are marked to skip on network timeouts (flaky CI)."""# CRITICAL: Must set TOKENIZERS_PARALLELISM before any imports@@ -16,9 +19,11 @@ import osos.environ["TOKENIZERS_PARALLELISM"] = "false"+import functoolsimport tempfilefrom pathlib import Path+import httpximport pytestfrom headroom.memory.config import MemoryConfig@@ -26,6 +31,20 @@ from headroom.memory.core import HierarchicalMemoryfrom headroom.memory.models import Memory, ScopeLevelfrom headroom.memory.ports import MemoryFilter++def network_timeout_handler(func):+ """Decorator to skip tests on network timeouts (flaky CI)."""++ @functools.wraps(func)+ async def wrapper(*args, **kwargs):+ try:+ return await func(*args, **kwargs)+ except httpx.ReadTimeout:+ pytest.skip("Skipped due to network timeout (flaky CI)")++ return wrapper++# =============================================================================# Fixtures# =============================================================================@@ -60,6 +79,7 @@ class TestAddBatch:"""Tests for HierarchicalMemory.add_batch()."""@pytest.mark.asyncio+ @network_timeout_handlerasync def test_add_batch_basic(self, memory_system):"""Test basic batch addition."""memories_data = [diff --git a/tests/test_memory/test_easy.py b/tests/test_memory/test_easy.pyindex 48cf71cf1..824deb5be 100644--- a/tests/test_memory/test_easy.py+++ b/tests/test_memory/test_easy.py@@ -7,6 +7,9 @@ Tests cover:- Error handling and edge cases- Backend type switching- Resource cleanup++Note: These are integration tests that may hit external embedding APIs.+Tests are marked to skip on network timeouts (flaky CI)."""# CRITICAL: Must set TOKENIZERS_PARALLELISM before any imports that might@@ -18,10 +21,26 @@ os.environ["TOKENIZERS_PARALLELISM"] = "false"import tempfilefrom pathlib import Path+import httpximport pytestfrom headroom.memory.easy import Memory, MemoryResult++def network_timeout_handler(func):+ """Decorator to skip tests on network timeouts (flaky CI)."""+ import functools++ @functools.wraps(func)+ async def wrapper(*args, **kwargs):+ try:+ return await func(*args, **kwargs)+ except httpx.ReadTimeout:+ pytest.skip("Skipped due to network timeout (flaky CI)")++ return wrapper++# =============================================================================# Fixtures# =============================================================================@@ -165,6 +184,7 @@ class TestMemorySave:assert memory_id is not None@pytest.mark.asyncio+ @network_timeout_handlerasync def test_save_with_entities(self, memory_instance):"""Test saving with pre-extracted entities."""memory_id = await memory_instance.save(@@ -455,6 +475,7 @@ class TestMemoryClose:assert mem._initialized is False@pytest.mark.asyncio+ @network_timeout_handlerasync def test_close_idempotent(self, temp_db_path):"""Test that close can be called multiple times."""mem = Memory(backend="local", db_path=temp_db_path)