Context Cache Usage Report
Executive Summary
This report analyzes how agents and components access interaction, session, and user contexts. The analysis confirms that agents use cache-only context, but identifies one area where session context generation queries the database directly (which is acceptable since it only runs at session end).
Context Access Flow
1. Context Retrieval Pattern
Orchestrator β Context Manager β Cache β Agents
orchestrator.process_request()
ββ> _get_or_create_context()
ββ> context_manager.manage_context()
ββ> Check session_cache (cache-first)
ββ> Check user_cache (cache-first)
ββ> Only queries DB if cache miss
ββ> Returns cached context to orchestrator
ββ> Passes to agents
2. Cache-Only Access Verification
All agents receive context from orchestrator, which gets it from cache:
Intent Agent (src/agents/intent_agent.py)
- Context Source: From orchestrator (line 201-204)
- Context Access:
- Uses
combined_context(pre-formatted from cache) - Falls back to
interaction_contextsanduser_contextfrom cache - Status: β Cache-only
- Uses
Skills Identification Agent (src/agents/skills_identification_agent.py)
- Context Source: From orchestrator (line 218-221)
- Context Access:
- Uses
user_contextfrom cache (line 230) - Uses
interaction_contextsfrom cache (line 231) - Status: β Cache-only
- Uses
Synthesis Agent (src/agents/synthesis_agent.py)
- Context Source: From orchestrator (line 283-287)
- Context Access:
- Uses
interaction_contextsfrom cache (lines 299, 358, 550) - Uses
user_contextfrom cache (implied in context dict) - Status: β Cache-only
- Uses
Safety Agent (src/agents/safety_agent.py)
- Context Source: From orchestrator (line 314-317)
- Context Access:
- Uses
user_contextfrom cache (line 158) - Uses
interaction_contextsfrom cache (line 163) - Status: β Cache-only
- Uses
Context Manager Cache Behavior
Session Context (Interaction Contexts)
Location: src/context_manager.py - manage_context() (lines 235-289)
Cache Strategy:
- Check cache first (line 247):
session_context = self._get_from_memory_cache(session_cache_key) - Query database only if cache miss (line 260-262): Only when
not session_context - Cache immediately after DB query (line 265): Warm cache with fresh data
- Update cache synchronously (line 444): When interaction context generated, cache updated immediately via
_update_cache_with_interaction_context()
Result: β Cache-first, DB only on miss
User Context
Location: src/context_manager.py - manage_context() (lines 267-281)
Cache Strategy:
- Check cache first (line 258):
user_context = self._get_from_memory_cache(user_cache_key) - Query database only if not cached (line 269):
if not user_context or not user_context.get("user_context_loaded") - Cache after first load (line 277):
self._warm_memory_cache(user_cache_key, user_context) - Never queries DB again (line 279-281): Uses cached user context for all subsequent requests
Result: β Load once, cache forever (no DB queries after initial load)
Interaction Contexts
Location: src/context_manager.py - _update_cache_with_interaction_context() (lines 770-809)
Cache Strategy:
- Updated synchronously with database (line 444): Called immediately after DB insert
- Adds to cached list (line 788): Updates in-memory cache without DB query
- Maintains most recent 20 (line 789): Matches database query limit
Result: β Cache updated when DB updated, no DB query needed
Session Context Analysis
Session Context Generation
Location: src/context_manager.py - generate_session_context() (lines 463-532)
Purpose: Generate 100-token summary of entire session for long-term user persona building
When Called: Only at session end via end_session() (line 540)
Database Access:
- β Queries database directly (lines 469-479) to get all interaction contexts for session
- Why This Is Acceptable:
- Only called once per session (at end)
- Not used by agents during conversation
- Used for generating user persona summary (long-term context)
- Cache doesn't need to maintain full session summary during conversation
Result: β Acceptable - Only runs at session end, not during conversation
Session Context Usage
Finding: Session context is NOT used by agents during conversation.
Evidence:
- No agent accesses
session_contextorsession_summaryfields - Session context is only used for generating user persona (
get_user_context()) - User persona is generated from session contexts across all sessions (line 326)
Current Usage:
- Generated at session end (
generate_session_context()) - Stored in
session_contextstable - Retrieved when generating user persona (
get_user_context()line 326) - User persona is cached after first load (lines 277-281)
Recommendation: β Current usage is correct - session context should not be accessed during conversation
Cache Update Synchronization
Interaction Context Updates
Location: src/context_manager.py - generate_interaction_context() (lines 422-447)
Flow:
- Generate interaction summary via LLM
- Store in database (line 427-438)
- Immediately update cache (line 444):
_update_cache_with_interaction_context() - Cache contains new interaction context before next request
Result: β Cache and database synchronized at write time
Verification Summary
β All Agents Use Cache-Only Context
| Agent | Context Source | Access Method | Cache-Only? |
|---|---|---|---|
| Intent Agent | Orchestrator | combined_context, interaction_contexts, user_context |
β Yes |
| Skills Agent | Orchestrator | user_context, interaction_contexts |
β Yes |
| Synthesis Agent | Orchestrator | interaction_contexts, user_context |
β Yes |
| Safety Agent | Orchestrator | user_context, interaction_contexts |
β Yes |
β Context Manager Cache Strategy
| Context Type | Cache Strategy | DB Queries |
|---|---|---|
| Interaction Contexts | Cache-first, updated on write | Only on cache miss |
| User Context | Load once, cache forever | Only on first load |
| Session Context | Generated at session end | Only at session end (acceptable) |
β Cache Synchronization
| Operation | Cache Update | Timing |
|---|---|---|
| Interaction Context Generated | β Immediate | Same time as DB write |
| Session Context Generated | β Not needed | Only at session end |
| User Context Loaded | β Immediate | After first DB query |
Findings
β Correct Behaviors
- Agents receive context from cache: All agents get context from orchestrator, which retrieves from cache
- Cache-first retrieval: Context manager checks cache before querying database
- User context cached forever: Loaded once, never queries database again
- Interaction contexts updated synchronously: Cache updated when database is updated
- Session context properly scoped: Only generated at session end, not used during conversation
β οΈ Session Context Notes
Session context generation queries database:
generate_session_context()directly queriesinteraction_contextstable (lines 473-479)- Status: β Acceptable - Only called at session end
- Impact: None - Not used during conversation flow
- Recommendation: No change needed
Session context not in cache during conversation: Session summaries are not cached during conversation
- Status: β Correct behavior
- Reason: Session summaries are 100-token summaries of entire session, not needed during conversation
- Usage: Only used for generating user persona (long-term context)
Recommendations
β No Changes Needed
All agents and components correctly use cache-only context during conversation. The only database query during conversation flow is:
- Initial cache miss (acceptable - only happens once per session or on cache expiration)
- User context first load (acceptable - only happens once per user)
Session context generation queries database, but this is acceptable because:
- Only runs at session end
- Not used by agents during conversation
- Used for long-term user persona building
Conclusion
Status: β All agents and components correctly use cache-only context
The system follows a cache-first strategy where:
- Context is retrieved from cache
- Database is only queried on cache miss (first request or cache expiration)
- Cache is updated immediately when database is updated
- User context is loaded once and cached forever
- Session context generation is properly scoped (session end only)
No changes required - system is working as designed.