Commit graph

118 commits

Author SHA1 Message Date
chopratejas
fb4117f3a6 Add Click-based CLI with memory management commands
Refactor CLI from argparse to Click for better extensibility:
- New headroom/cli/ package with modular command structure
- Memory commands: list, show, stats, edit, delete, prune, purge, export, import
- Rich terminal output with tables, colors, and formatted stats
- Duration parsing for --older-than and --since flags (7d, 2w, 1m)
- Comprehensive tests using Click's CliRunner (55 tests, no mocks)

CLI structure:
  headroom proxy        - Start optimization proxy (migrated from argparse)
  headroom memory ...   - Memory management (new)
  headroom evals ...    - Evaluation commands (migrated, now under evals group)

Backwards compatibility maintained via headroom/cli.py shim.
2026-01-29 21:30:21 -08:00
Tejas Chopra
0f2a0d93c2
Merge pull request #12 from chopratejas/feature/bedrock-backend
Add cloud provider support via LiteLLM backend
2026-01-29 16:30:46 -08:00
chopratejas
f4c813ea8e Add cloud provider support via LiteLLM backend
Enables Headroom proxy to work with AWS Bedrock, Google Vertex AI,
Azure OpenAI, and 100+ other providers via LiteLLM.

Usage:
  headroom proxy --backend bedrock --region us-west-2
  headroom proxy --backend vertex_ai --region us-central1
  headroom proxy --backend azure --region eastus

Features:
- Automatic format translation (Anthropic API <-> provider APIs)
- Streaming support with proper SSE event translation
- Uses existing cloud credentials (AWS, GCP, Azure)
- Shorthand: --backend bedrock (expands to litellm-bedrock)
- Stats tracking per provider
2026-01-29 16:12:10 -08:00
chopratejas
4ea173388a Add global httpx.ReadTimeout handler for memory tests
Use pytest hook to catch httpx.ReadTimeout and skip tests instead of
failing. This handles flaky network timeouts from HuggingFace Hub
during sentence-transformers model downloads in CI.

The hook covers all tests in tests/test_memory/ directory.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-28 09:43:44 -08:00
chopratejas
52da662979 Fix mypy errors and add network timeout handler for flaky CI tests
Mypy fixes (no-any-return errors from external libraries):
- litellm_pricing.py: cast litellm.model_cost
- anthropic.py: cast litellm cost returns
- cohere.py: cast litellm info/cost returns
- compressor.py: explicit int() for PIL size calculations
- sqlite.py: explicit bytes() for numpy tobytes()
- universal.py: explicit str() for CCR store key
- direct_mem0.py: explicit list() for OpenAI embedding
- langchain/agents.py: explicit str() for result
- server.py: explicit str() for httpx response.text
- runner_v2/v3.py: add hasattr check for backend.close()

Test fixes (flaky network timeouts in CI):
- Add network_timeout_handler decorator to skip on httpx.ReadTimeout
- Applied to test_close_idempotent, test_save_with_entities, test_add_batch_basic

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 19:39:33 -08:00
chopratejas
67631fecfc chore: add mypy to pre-commit hooks 2026-01-27 17:01:46 -08:00
chopratejas
58cc207f29 fix: resolve mypy type errors
- Add Transform type annotation for context_manager in proxy/server.py
- Import Transform from headroom.transforms
- Fix response.content[0].text access in judge.py with hasattr check
- Add _neo4j_driver type annotation in direct_mem0.py
2026-01-27 16:58:11 -08:00
chopratejas
a187d80d7c fix: resolve CI failures
- Format feature_extractor.py to pass ruff format check
- Skip uvicorn tests when uvicorn not installed in CI environment
- Exclude experiments/ from pre-commit ruff checks
2026-01-27 16:17:24 -08:00
chopratejas
d57673d745 feat(cli): add IntelligentContext CLI arguments
- Add --no-intelligent-context to disable IntelligentContextManager
- Add --no-intelligent-scoring to disable multi-factor scoring
- Add --no-compress-first to disable compression before dropping
- Update README to reflect IntelligentContext as the default context manager
2026-01-27 16:08:36 -08:00
chopratejas
d3298368bf fix: improve error handling and add comprehensive test coverage
Bug fixes:
- Replace bare except handlers with specific exception types and logging
  in proxy/server.py (6 instances for CCR, SSE parsing, cost tracking)
- Fix session_id filtering security bug in memory/backends/local.py
  (sessions were not properly isolated in vector search)

New tests (344 total):
- test_ccr_batch_processor.py: 51 tests for batch result processing
- test_compression_store.py: 76 tests for compression cache
- test_log_compressor.py: 47 tests for log format detection/compression
- test_search_compressor.py: 48 tests for grep output compression
- test_integrations/langchain/: 122 tests for LangChain integration
  (agents, memory, retriever, streaming)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 16:08:36 -08:00
chopratejas
83c0334ccd docs: update documentation for IntelligentContext TOIN + CCR integration
Updates across multiple docs to reflect the new message-level compression
with TOIN + CCR integration:

- docs/ccr.md: Add CCR-enabled components table, message-level CCR section
- docs/ARCHITECTURE.md: Expand Transform 6 with TOIN + CCR integration details
- docs/configuration.md: Add CCR integration config and marker format
- docs/proxy.md: Add CCR integration note for context management
- docs/README.md: Update to reference IntelligentContextManager as default

Also adds examples/test_intelligent_context_toin_ccr.py for scale testing
the TOIN + CCR integration with real API calls.
2026-01-27 16:08:36 -08:00
chopratejas
fef02fa053 feat(toin): add TOIN + CCR integration to IntelligentContextManager
IntelligentContext is a message-level compressor that drops low-value
messages. This change adds bidirectional TOIN integration:

- Dropped messages stored in CCR for potential retrieval
- Drops recorded to TOIN for cross-user learning
- Retrieval feedback improves future importance scoring

When messages are dropped and users retrieve them via CCR, TOIN learns
to score those patterns higher next time. This creates a feedback loop
that improves drop decisions across all users.

Changes:
- Add _create_message_signature() for TOIN pattern tracking
- Add _get_compression_store() for CCR integration
- Add _store_dropped_in_ccr() to store dropped messages
- Add _record_drops_to_toin() to record drops for learning
- Update marker to include CCR reference when available
- Update docs with TOIN + CCR integration section
- Update tests to accept both marker formats
2026-01-27 16:08:36 -08:00
chopratejas
4f9a3d1e05 feat(toin): extend TOIN integration to all ContentRouter compression strategies
Previously, only SmartCrusher (JSON arrays) recorded compressions to TOIN.
Now all compression strategies record to TOIN for cross-user learning:

- CODE_AWARE: AST-based code compression
- SEARCH: grep/ripgrep results
- LOG: build/test output
- LLMLINGUA: ML-based text compression
- TEXT: heuristic text compression

Changes:
- Add _create_content_signature() for non-JSON content types
- Add _record_to_toin() method to ContentRouter
- Update _apply_strategy_to_content() to record after compression
- Update docs/transforms.md with TOIN integration section

This enables the feedback loop where TOIN learns from retrieval patterns
across all content types, not just JSON arrays.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-27 16:08:36 -08:00
chopratejas
7c6d713ae0 feat(proxy): add connection pooling, HTTP/2, and multi-worker support
Improve proxy scalability for high-concurrency scenarios with multiple agents:

- Add connection pool configuration (max_connections=500, max_keepalive=100)
- Enable HTTP/2 multiplexing by default for better throughput
- Add multi-worker support via --workers flag for multi-core scaling
- Add --limit-concurrency flag for backpressure control
- Reuse httpx client for CCR continuations instead of creating new clients
- Add httpx[http2] dependency for HTTP/2 support
- Add comprehensive test suite for scalability features

New CLI flags:
  --max-connections    Max connections to upstream APIs (default: 500)
  --max-keepalive      Max keepalive connections (default: 100)
  --no-http2           Disable HTTP/2 (enabled by default)
  --workers            Number of worker processes (default: 1)
  --limit-concurrency  Max concurrent connections before 503 (default: 1000)
2026-01-27 16:08:36 -08:00
Tejas Chopra
9825d993ba feat: add streaming memory tool support with credential error handling
- Implement streaming memory tool detection and execution for Anthropic API
- Buffer SSE response to detect tool_use blocks, execute tools, and stream continuation
- Add helpful error detection and messaging for subscription credential restrictions
- Add startup note when memory tools enabled warning about API key requirement
- Move hnswlib to core dependencies for memory system
- Update CLI to show memory tool/context status on startup
2026-01-27 00:05:51 -08:00
Tejas Chopra
787f925204 fix: correct cost calculation for cache tokens and simplify memory DevEx
- Fix Anthropic cache token cost formula: input_tokens, cache_read, and
  cache_write are all separate (not overlapping), so don't subtract
  cache_write from input_tokens
- Default memory user ID to "default" when x-headroom-user-id header is
  not provided, removing the need for client configuration
- Update CLI help text and tests to reflect new default behavior
2026-01-26 22:49:31 -08:00
Tejas Chopra
74872b203b fix: resolve mypy type errors across codebase
- Add type annotations for variables with inferred Any types
- Add close() method to MemoryBackend protocol
- Fix union type assignments in eval runners
- Add explicit casts for return type mismatches
- Fix None callable issues with assertions
- Regenerate uv.lock to fix corrupted pillow dependency
2026-01-26 22:41:59 -08:00
chopratejas
da74341858 Add hierarchical memory system with graph + vector storage
Implement comprehensive memory system supporting:
- Local backend (SQLite + FTS5 + HNSW) for zero-dependency operation
- Mem0 backends (Neo4j + Qdrant) for production graph memory
- DirectMem0Adapter for optimized pre-extracted data (bypasses LLM)
- Memory extraction with facts, entities, and relationships
- Proxy integration with --memory flag for automatic memory injection

Key components:
- headroom/memory/backends/: LocalBackend, Mem0Backend, DirectMem0Adapter
- headroom/memory/system.py: MemorySystem with tool-based interface
- headroom/memory/extraction.py: Entity and relationship extraction
- headroom/proxy/memory_handler.py: Proxy integration layer
- headroom/prediction/feature_extractor.py: Content analysis features

Testing:
- 217 new memory system tests covering all backends
- LoCoMo evaluation framework for memory quality assessment
- Integration tests for proxy memory functionality

Also removes deprecated example files in favor of focused test coverage.
2026-01-26 21:58:47 -08:00
Tejas Chopra
8c22a72715 fix: parse actual token usage from SSE streams instead of byte estimation
The proxy was estimating output tokens by dividing total streamed bytes
by 4, which was wildly inaccurate for SSE streams due to protocol
overhead (event headers, JSON field names, etc.). This caused ~18x
inflation in reported output tokens and costs.

Changes:
- Add _parse_sse_usage() to extract actual usage from SSE events
- Anthropic: Parse message_start (input) and message_delta (output)
- OpenAI: Parse usage object from final chunk
- Gemini: Parse usageMetadata from streaming chunks
- Inject stream_options.include_usage=true for OpenAI streaming
- Account for cached tokens in streaming cost calculations
- Use conservative fallback (÷40) when no usage data available
2026-01-26 20:57:15 -08:00
Tejas Chopra
6b1cd38c75 Add missing pillow dependency for image compression
The image compression module uses PIL for resizing images
(Anthropic/Google providers) but pillow was not listed in
dependencies, causing CI failures.
2026-01-25 22:49:04 -08:00
Tejas Chopra
2fd9552102 Add image token compression with trained ML router
Introduces automatic image compression for LLM requests, reducing token
usage by 40-90% while maintaining answer accuracy.

Key features:
- Trained MiniLM classifier (93.7% accuracy) hosted on HuggingFace
- SigLIP-based image analysis for content-aware routing
- Provider-specific compression:
  - OpenAI: detail="low" parameter
  - Anthropic: PIL resize to 512px
  - Google: PIL resize to 768px (tile-optimized)
- Four compression techniques: full_low, preserve, crop, transcode
- Integration in both Headroom proxy and SDK (ContentRouter)

New files:
- headroom/image/ module with ImageCompressor API
- docs/image-compression.md user documentation
- tests/test_image_compressor.py (51 tests)

Model: chopratejas/technique-router on HuggingFace (~128MB)
2026-01-25 22:40:43 -08:00
Tejas Chopra
6bb35ebe75 fix: account for cached tokens in cost calculation across all providers
Previously, the proxy treated all input tokens as full-price,
ignoring cached token discounts from provider responses.

This caused reported costs to be significantly higher than actual
billing, especially for long conversations with high cache hit rates.

Changes:
- Anthropic: Parse cache_read_input_tokens (90% discount)
- OpenAI: Parse prompt_tokens_details.cached_tokens (50% discount)
- Gemini: Parse cachedContentTokenCount (75-90% discount)
- CostTracker: Use LiteLLM's cache_read_input_token_cost when available
- Streaming: Added TODO for SSE parsing (currently estimates only)

Example: 10K input + 90K cached + 5K output on Opus 4.5
- Before: $0.625 (all tokens at full price)
- After:  $0.220 (cached at discounted rate)
2026-01-25 20:34:20 -08:00
chopratejas
62ee1dc9e3 Add E2E tests for Google multimodal content preservation
- Tests for text-only, image, function calling, function response, mixed conversation
- Tests skip automatically when GOOGLE_API_KEY not set (CI-safe)
- Can run standalone: GOOGLE_API_KEY=key python tests/test_google_multimodal_e2e.py

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 21:01:25 -08:00
chopratejas
dd4c65e565 Fix Google Gemini conversion to preserve non-text content (Gap #4)
Previously, _gemini_contents_to_messages() only extracted text parts,
silently dropping non-text content like images, file references, and
function calls. This caused data loss when processing multimodal content.

Changes:
- Add _has_non_text_parts() helper to detect inlineData, fileData,
  functionCall, and functionResponse parts
- Modify _gemini_contents_to_messages() to return tuple of (messages,
  preserved_indices) tracking which content entries have non-text parts
- Update all call sites to preserve non-text content entries:
  - handle_google_batch_create
  - handle_gemini_generate_content
  - handle_gemini_count_tokens
  - _store_google_batch_context
- Skip compression entirely when all content has non-text parts
- Restore preserved entries after compression/optimization

Add comprehensive test coverage (54 tests) verifying:
- Detection of all non-text part types
- Correct index tracking in preserved_indices
- Role mapping and message conversion
- Realistic conversation flows with images, function calls, PDFs
2026-01-24 12:20:13 -08:00
chopratejas
80bfb1267b Fix mypy type checking errors in CCR batch modules
- batch_processor.py: Add explicit type annotations for dict return values,
  use isinstance checks for nested dict access, annotate response.json() results
- batch_compression_eval.py: Add return type annotations for _get_tokenizer
  and main(), type _tokenizer field as Any, fix variable shadowing in main()
- proxy/server.py: Add type: ignore comments for http_client union type issues,
  update handle_gemini_stream_generate_content return type to include JSONResponse
2026-01-24 12:01:58 -08:00
chopratejas
cec2b7617b Update compressor CCR markers and add batch compression eval
## CCR Marker Format Standardization

Updated all compressors to use a consistent CCR marker format that the
CCRToolInjector can reliably detect:

- LogCompressor: `[N lines compressed to M. Retrieve more: hash=xxx]`
- SearchCompressor: `[N matches compressed to M. Retrieve more: hash=xxx]`
- TextCompressor: `[N lines compressed to M. Retrieve more: hash=xxx]`

This matches the enhanced marker patterns added to tool_injection.py in
the previous commit, ensuring all compressors work with CCR retrieval.

## Batch Compression Eval Module

Added `headroom/evals/batch_compression_eval.py` for testing compression
accuracy with batch APIs:

- Evaluates whether compression preserves LLM accuracy
- Tests with known/verifiable answers (math, factual, JSON extraction)
- Compares compressed vs uncompressed results
- Reports accuracy preservation rate and token savings
- Works with both OpenAI and Anthropic batch APIs

Usage:
    from headroom.evals import run_batch_compression_eval
    results = run_batch_compression_eval(provider="anthropic", n_samples=10)
2026-01-24 11:42:44 -08:00
chopratejas
95fd6d8688 Add multi-provider batch API support with CCR post-processing
This commit adds comprehensive batch API support for all three major LLM
providers (Anthropic, OpenAI, Google/Gemini) with integrated CCR
(Compress-Cache-Retrieve) functionality for asynchronous batch processing.

## Batch CCR Post-Processing Architecture

When batch APIs are used, responses are processed asynchronously. If the
model calls the CCR retrieval tool (`headroom_retrieve`) within a batch
response, the system now handles this automatically:

1. **Batch Submit**: Request context (messages, tools, model) is stored
   in BatchContextStore keyed by batch_id
2. **Batch Results**: When results are retrieved, CCR tool calls are
   detected in the responses
3. **Continuation**: For each CCR tool call, the system executes local
   retrieval and makes a continuation API call to complete the response
4. **Result Update**: The batch result is updated with the complete
   response, transparent to the caller

## New Components

- `headroom/ccr/batch_store.py`: TTL-based context storage for batch
  requests, enabling CCR retrieval during result processing
- `headroom/ccr/batch_processor.py`: Processes batch results, detects
  CCR tool calls across all provider formats, executes continuations

## Provider Support

### Anthropic
- POST /v1/messages/batches (create with compression)
- GET /v1/messages/batches (list)
- GET /v1/messages/batches/{id} (status)
- GET /v1/messages/batches/{id}/results (with CCR post-processing)

### OpenAI
- POST /v1/batches (create)
- GET /v1/batches (list)
- GET /v1/batches/{id} (status)
- Batch file upload/download support

### Google/Gemini
- Native API support: /v1beta/models/{model}:generateContent
- Batch API: /v1beta/models/{model}:batchGenerateContent
- Token counting: /v1beta/models/{model}:countTokens
- OpenAI-compatible endpoint support

## CCR Enhancements

- Added Google/Gemini format support to response_handler.py
- Extended tool_injection.py with multiple marker patterns for different
  compressors (SmartCrusher, TextCompressor, LogCompressor, etc.)
- Added Google functionCall/functionResponse handling

## Proxy Server Updates

- Added Gemini native API handlers alongside OpenAI-compatible endpoints
- Integrated batch context storage on submission
- Added batch result processing with CCR continuation
- Rate limiting and metrics tracking for all providers

## Test Coverage

Added comprehensive integration tests (all skip gracefully without API keys):
- test_proxy_batch_integration.py: Anthropic and OpenAI batch APIs
- test_proxy_gemini_integration.py: Gemini via OpenAI-compatible endpoint
- test_proxy_gemini_native_integration.py: Gemini native API
- test_proxy_count_tokens_integration.py: Token counting endpoint
- test_proxy_openai_responses_integration.py: OpenAI responses API
- test_proxy_passthrough_integration.py: Passthrough endpoints

## Compression Results (Real API Testing)

- Token savings: 83-98% on tool result content
- CCR tool injection: Working across all providers
- Model behavior: OpenAI gpt-4o-mini successfully called headroom_retrieve
  when presented with compressed data, proving end-to-end CCR functionality
2026-01-24 11:41:18 -08:00
chopratejas
0b3e4d2586 Remove hardcoded source hint system from ContentRouter
ContentRouter now routes purely based on content analysis instead of
relying on hardcoded tool name mappings. This makes the router work
with any MCP tool regardless of naming convention.

Changes:
- Remove generate_source_hint() function and _strategy_from_hint() method
- Remove source_hint parameter from compress() method
- Remove _get_tool_source_hint() from IntelligentContextManager
- Update tests to remove source hint test cases
- Update docs to document content detection approach
2026-01-23 00:28:22 -08:00
chopratejas
dcae408dcd Add hnswlib to dev dependencies for CI testing
- Add hnswlib>=0.8.0 to dev dependencies so CI can run HNSW tests
- Add new 'memory' extras group for users who want vector search
- Include 'memory' in 'all' extras
2026-01-22 23:58:54 -08:00
chopratejas
df6a38b477 Make hnswlib optional and skip tests when unavailable
- Wrap hnswlib import in try/except in hnsw.py
- Export HNSW_AVAILABLE flag from adapters module
- Add helpful error message when HNSWVectorIndex is used without hnswlib
- Add @pytest.mark.skipif to HNSW test classes

hnswlib requires C++ compilation and may not be available on all
platforms or Python versions in CI environments.
2026-01-22 23:54:21 -08:00
chopratejas
8c88d49172 Update memory documentation for HierarchicalMemory system
- Update Quick Start to use with_memory() API (replaces with_fast_memory)
- Add hierarchical scoping section (USER → SESSION → AGENT → TURN)
- Add temporal versioning section with supersession examples
- Add comparison with state of the art (Letta, Mem0) with feature matrices
- Document Memory API for direct access (.memory.search, .add, .get_all)
- Add advanced HierarchicalMemory API usage examples
- Update configuration options for embedders and storage
- Add protocol-based architecture diagram
- Update memory categories (PREFERENCE, FACT, CONTEXT, ENTITY, DECISION, INSIGHT)
- Add troubleshooting and best practices sections
2026-01-22 23:37:14 -08:00
chopratejas
c850ccc3b2 Replace legacy memory system with HierarchicalMemory
Major refactor of the memory module:

- Add hierarchical scoping (user → session → agent → turn)
- Add temporal versioning with supersession support
- Add pluggable adapters (SQLite store, HNSW vectors, FTS5 text search)
- Add protocol interfaces (ports) for all memory components
- Update LRUMemoryCache to implement async MemoryCache protocol
- Update wrapper.py to use HierarchicalMemory backend
- Preserve with_memory() one-liner API with zero-latency inline extraction

New files:
- adapters/: sqlite.py, hnsw.py, fts5.py, cache.py, embedders.py
- core.py: HierarchicalMemory orchestrator
- models.py: Memory, MemoryCategory, ScopeLevel
- ports.py: Protocol interfaces (MemoryStore, VectorIndex, etc.)
- config.py: MemoryConfig with backend selection
- factory.py: Component creation from config

Removed legacy files:
- store.py, fast_store.py, extractor.py, worker.py, fast_wrapper.py

Breaking change: Removes legacy memory API (pre-0.3.0)
2026-01-22 23:28:21 -08:00
chopratejas
92e4a24ea7 Add comprehensive evaluation framework for compression accuracy
- Add headroom.evals module with 12+ dataset loaders (HotpotQA, SQuAD,
  Natural Questions, TriviaQA, MS MARCO, LongBench, NarrativeQA, BFCL,
  ToolBench, CodeSearchNet, HumanEval, built-in tool outputs)
- Add before/after evaluation runner that compares LLM responses with
  original vs compressed context
- Add metrics: F1 score, semantic similarity, exact match, ground truth
- Add CLI: python -m headroom.evals quick|benchmark|list|report
- Add [evals] extra to pyproject.toml for pip install headroom-ai[evals]

Fix ContentRouter to use LLMLingua for plain text compression:
- Route TEXT strategy through LLMLingua instead of heuristic TextCompressor
- Adjust LLMLingua compression rates for better accuracy (0.5 vs 0.25)
- HotpotQA now achieves 95% accuracy with 44% compression

Update documentation with evaluation framework section

Fix test isolation in test_toin.py (TOIN singleton persistence)
2026-01-22 09:31:54 -08:00
chopratejas
4b04a45736 Add pre-commit hooks for ruff linting and formatting
- Add .pre-commit-config.yaml with ruff and ruff-format hooks
- Add pre-commit to dev dependencies
- Ignore UP038 rule (tuple syntax in isinstance is clearer)

Run `uv run pre-commit install` to enable hooks locally.
2026-01-21 21:49:29 -08:00
chopratejas
fa5ba8f1e1 Update test for lowered TOIN confidence threshold default 2026-01-21 21:40:11 -08:00
chopratejas
018b1ba4b9 Fix TOIN integration and add persistence support
Enable TOIN (Tool Output Intelligence Network) to work end-to-end
through the proxy by fixing content routing and adding persistence.

Changes:
- Fix ContentRouter to route json_array hint to SmartCrusher
  (prevents JSON arrays with text from being misclassified as mixed)
- Add default TOIN storage path (~/.headroom/toin.json)
- Support HEADROOM_TOIN_PATH env var for custom storage location
- Add TOIN API endpoints: /v1/toin/stats, /v1/toin/patterns,
  /v1/toin/pattern/{hash_prefix}
- Lower min_samples threshold from 10 to 3 for faster learning
- Add progressive confidence based on sample count
- Add debug logging for TOIN hint application
- Add comprehensive integration tests (no mocks)
2026-01-21 21:24:09 -08:00
chopratejas
4ac48ed443 Add reasoning capability forwarding to HeadroomAgnoModel
- Add underlying_model property for framework introspection
- Forward capability attributes (thinking, reasoning_effort, etc.) from wrapped model
- Add has_extended_thinking_enabled() method for detecting extended thinking config
- Update docstring with guidance on reasoning mode compatibility
- Add 17 unit tests for reasoning capability forwarding
2026-01-21 20:32:02 -08:00
chopratejas
2292724ff7 Add dynamic anchor selection with content deduplication
Replace static "first 3 + last 2" preservation with intelligent anchor
selection that adapts to data patterns and array size.

Key changes:
- Add AnchorSelector class for dynamic position-based preservation
- Add AnchorConfig for configurable anchor allocation (budget ratio,
  strategy weights, information density scoring)
- Add content-based deduplication to prevent wasting slots on identical
  items using SHA256 hashing
- Add _fill_remaining_slots() to maximize output when dedup reduces items
- Support data pattern detection (TIME_SERIES, SEARCH_RESULTS, LOGS, GENERIC)
- Support query-aware anchor adjustment for back-heavy patterns

Enterprise hardening:
- Thread-safe: No shared state modified
- O(n) performance for dedup and slot filling
- Fault-tolerant serialization with fallbacks
- Configurable via dedup_identical_items flag

52 tests covering adversarial positions, size adaptation, pattern-aware
anchoring, query-aware selection, information density, coverage metrics,
edge cases, and preservation guarantees.
2026-01-21 00:19:05 -08:00
chopratejas
3ffe68618a Add pluggable storage backend abstraction for CompressionStore
- Add CompressionStoreBackend protocol for duck-typed backends
- Add InMemoryBackend as default thread-safe implementation
- Refactor CompressionStore to accept optional backend parameter
- Add comprehensive backend contract tests (28 tests)
2026-01-20 23:25:28 -08:00
Tejas Chopra
029ca7d7e8 Add demo GIF to README 2026-01-20 18:57:17 -08:00
Tejas Chopra
4e944db1d2 Bump version to 0.2.15 2026-01-20 18:00:30 -08:00
Tejas Chopra
5ef1d4a931 Fix streaming responses not tracking costs
The streaming code path in _stream_response was missing cost calculation
and recording. While non-streaming requests properly calculated costs via
CostTracker.estimate_cost() and passed them to metrics.record_request(),
streaming requests always passed the default value of 0.

This caused session summaries to show $0.00 for total cost and savings
even when using expensive models like Claude Opus.

Added cost calculation logic to the streaming finally block, matching
the non-streaming implementation.
2026-01-20 17:34:14 -08:00
chopratejas
cd5ea2ea1d Add architecture diagrams to README and docs 2026-01-20 00:44:55 -08:00
chopratejas
ec56092555 Add PyPI downloads badge to README 2026-01-19 23:51:31 -08:00
chopratejas
2ce26438a0 Integrate DynamicContentDetector into CacheAligner (Phase 1)
- Add DynamicContentDetector integration for comprehensive dynamic content
  detection (20+ patterns vs previous 4 date patterns)
- New detection: UUIDs, API keys, JWT tokens, Unix timestamps, request/trace
  IDs, hex hashes (MD5/SHA1/SHA256), version numbers, high-entropy strings
- Add CacheAlignerConfig options: use_dynamic_detector, detection_tiers,
  extra_dynamic_labels, entropy_threshold
- Maintain backward compatibility with legacy date-only mode
- Add 25 new comprehensive tests for Phase 1 functionality
- Fix code compressor fallback test to properly mock LLMLingua availability

Expected cache hit improvement: 30-50% by extracting more dynamic content
2026-01-19 22:56:20 -08:00
Tejas Chopra
6ec85f3cbd Fix cost tracking to use LiteLLM pricing database correctly
- Remove hardcoded fallback pricing dictionary
- Fix litellm API usage: use cost_per_token() instead of broken
completion_cost() call (API changed, old kwargs no longer work)
- Add proper logging when pricing lookup fails
- Handle cached tokens at 10% of input price

The previous code silently failed because litellm.completion_cost()
no longer accepts prompt_tokens/completion_tokens kwargs, causing
all cost calculations to return $0.00.
2026-01-19 22:47:52 -08:00
chopratejas
dc72670bce Add demo video link to README 2026-01-19 22:28:12 -08:00
chopratejas
bd2d447c26 Add quality retention eval and fix linting for Python 3.12
- Add quality_retention_eval.py for needle-in-haystack testing to verify
  intelligent compression retains critical information (100% retention achieved)
- Add intelligent_context_integration_test.py for comprehensive pipeline testing
- Add test_progressive_summarizer.py with 36 tests for ProgressiveSummarizer
- Add HeadroomConfig parameter to HeadroomClient for direct config injection
- Update pipeline.py with IntelligentContextManager wiring and logging
- Fix all ruff linting issues and format for Python 3.12 compatibility
- Add comprehensive_eval.py benchmark for multi-scenario evaluation
- Add real_data_demo.py for production-scale volume testing
- Add reasoning agent test examples (groq, debug)
2026-01-19 21:52:18 -08:00
chopratejas
d48f479882 Bump version to 0.2.14 2026-01-19 20:37:17 -08:00
Tejas Chopra
f5ffba61a7 Merge pull request #8 from nicolabeghin/proxy-custom-openai-api-url
Add OpenAI API URL override option
2026-01-19 17:58:11 -08:00