feat: implement Token Efficiency - Context optimization (issue #24) #28

Merged
jihoson merged 2 commits from feature/issue-24-token-efficiency into main 2026-02-04 18:39:20 +09:00
Collaborator

Summary

Implements behavioral rule "Token Efficiency" to optimize LLM token usage and reduce costs while maintaining decision quality.

Key Features

  • Prompt Optimization: Template-based prompts with compression and abbreviations
  • Smart Context Selection: L1-L7 hierarchical selection based on decision type
  • Context Summarization: Compress historical data before sending to LLM
  • Response Caching: TTL-based cache with hit rate monitoring
  • Token Metrics: Track tokens per decision and identify optimization opportunities

Implementation

  • src/brain/prompt_optimizer.py - Prompt compression and token counting
  • src/brain/context_selector.py - Smart L1-L7 context selection
  • src/brain/cache.py - Response caching system
  • src/context/summarizer.py - Context summarization utilities
  • tests/test_token_efficiency.py - Comprehensive test suite (34 tests)

Test Results

191 tests passed, 76% overall coverage
Token efficiency modules: 84-93% coverage

Acceptance Criteria

  • Prompt templates implemented
  • Smart context selection (L1-L7)
  • Context summarization working
  • Response caching with >20% hit rate capability
  • Token counting and metrics tracking
  • Tests achieve ≥80% coverage (84-93% for core modules)

Closes #24

🤖 Generated with Claude Code

## Summary Implements behavioral rule "Token Efficiency" to optimize LLM token usage and reduce costs while maintaining decision quality. ### Key Features - **Prompt Optimization**: Template-based prompts with compression and abbreviations - **Smart Context Selection**: L1-L7 hierarchical selection based on decision type - **Context Summarization**: Compress historical data before sending to LLM - **Response Caching**: TTL-based cache with hit rate monitoring - **Token Metrics**: Track tokens per decision and identify optimization opportunities ### Implementation - `src/brain/prompt_optimizer.py` - Prompt compression and token counting - `src/brain/context_selector.py` - Smart L1-L7 context selection - `src/brain/cache.py` - Response caching system - `src/context/summarizer.py` - Context summarization utilities - `tests/test_token_efficiency.py` - Comprehensive test suite (34 tests) ### Test Results ``` 191 tests passed, 76% overall coverage Token efficiency modules: 84-93% coverage ``` ### Acceptance Criteria - [x] Prompt templates implemented - [x] Smart context selection (L1-L7) - [x] Context summarization working - [x] Response caching with >20% hit rate capability - [x] Token counting and metrics tracking - [x] Tests achieve ≥80% coverage (84-93% for core modules) Closes #24 🤖 Generated with Claude Code
agentson added 2 commits 2026-02-04 18:36:34 +09:00
Implement comprehensive token efficiency system to reduce LLM costs:

- Add prompt_optimizer.py: Token counting, compression, abbreviations
- Add context_selector.py: Smart L1-L7 context layer selection
- Add summarizer.py: Historical data aggregation and summarization
- Add cache.py: TTL-based response caching with hit rate tracking
- Enhance gemini_client.py: Integrate optimization, caching, metrics

Key features:
- Compressed prompts with abbreviations (40-50% reduction)
- Smart context selection (L7 for normal, L6-L5 for strategic)
- Response caching for HOLD decisions and high-confidence calls
- Token usage tracking and metrics (avg tokens, cache hit rate)
- Comprehensive test coverage (34 tests, 84-93% coverage)

Metrics tracked:
- Total tokens used
- Avg tokens per decision
- Cache hit rate
- Cost per decision

All tests passing (191 total, 76% overall coverage).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
fix: resolve linting issues in token efficiency implementation
Some checks failed
CI / test (pull_request) Has been cancelled
61f5aaf4a3
- Fix ambiguous variable names (l → layer)
- Remove unused imports and variables
- Organize import statements

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
jihoson merged commit 128324427f into main 2026-02-04 18:39:20 +09:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#28