Repository navigation
Description
Problem
The agentic chat system (utils/retrieval/agentic.py) uses gpt-5.1 with 22 tool definitions via a ReAct agent. Token analysis reveals significant waste:
10 messages cost ~$0.71 (534K input tokens + 6.4K output tokens)
Where tokens go per message
| Component | Tokens per turn | × 3 turns/msg | % of total |
|---|---|---|---|
| Tool schemas (22 tools) | 11,100 | 33,300 | 69% |
| System prompt | 2,870 | 8,610 | 18% |
| Tool results | ~2,000 × 2 | 4,000 | 8% |
| Message history | ~1,750 | 5,250 | 5% |
Key findings
-
Tool schemas are 69% of input cost — 22 tools with full JSON schemas (~11,100 tokens) are re-sent on every ReAct turn. For 10 messages with ~3 turns each = 30 LLM calls, tool schemas alone = 333,000 tokens = $0.42.
-
No cross-request memory —
thread_idis a new UUID per request. All tool results are discarded after each message. If user asks "what about my calendar?" after "what did I do today?", the agent re-fetches everything from scratch. -
Safety guard limits are generous — 25 tool calls and 500K context tokens per query. A runaway agent could burn ~$0.65 on a single message.
-
3 pre-routing LLM calls per message —
extract_question+requires_context+goal_progress(all gpt-4.1-mini, ~$0.01/10 msgs — cheap but redundant). -
Heavy queries ("summarize my week") can hit $1.13 for 10 messages — 3-4 tool calls with 4K+ results each.
Token burn breakdown (10 agentic messages)
| Model | Input tokens | Output tokens | Cost |
|---|---|---|---|
| gpt-5.1 (agent) | 521,100 | 5,000 | $0.70 |
| gpt-4.1-mini (routing) | 13,500 | 1,400 | $0.01 |
| Total | 534,600 | 6,400 | $0.71 |
Cost at scale
At 10K DAU × 5 agentic messages/day:
- Daily: 10K × 5 × $0.071 = $3,550/day
- Monthly: $106,500/month just on chat
Proposed optimizations
P0 — Prompt caching (biggest win)
OpenAI's automatic prefix caching gives 90% discount on cached input tokens. Since system prompt + tool schemas (14K tokens) are identical across all requests, ensuring cache hits would save:
- 333K tool schema tokens × 90% = save ~$0.37 per 10 messages
- Potential 52% cost reduction
Requirements: stable prefix ordering (system prompt → tools → messages), minimum 1024 token prefix.
P1 — Tool schema reduction
- Trim verbose tool descriptions (many have 700-900 token descriptions with examples)
- Consider lazy tool loading — only include tools relevant to the question type
- Split into tool groups: core (conversations, memories) vs specialized (calendar, health, gmail)
P2 — Cross-request context reuse
- Persist tool results in session cache (Redis, TTL=5min)
- When user asks follow-up, reuse recent tool results instead of re-fetching
- Could eliminate 1-2 tool calls per follow-up message
P3 — Reduce ReAct turns
- Encourage single-tool-call patterns via prompt engineering
- Parallel tool calling (gpt-5.1 supports it) to reduce round trips
Files involved
backend/utils/retrieval/agentic.py— ReAct agent creation, streaming, safety guardbackend/utils/llm/chat.py:398-744—_get_agentic_qa_prompt()system promptbackend/utils/retrieval/tools/— All 22 tool definitionsbackend/utils/llm/clients.py:47-54—llm_agent/llm_agent_stream(gpt-5.1)backend/utils/retrieval/safety.py— Safety guard (25 calls, 500K tokens)backend/utils/retrieval/graph.py:119-151— Routing logic
Related
- Migrate mentor notification topic extraction from gpt-4 to gpt-4.1-mini #4671 (mentor notifications gpt-4 → gpt-4.1-mini migration)
Activity
fix: optimize agentic chat prompt caching — 62% cost reduction (#4676) (
fix: optimize agentic chat prompt caching — 62% cost reduction (Based…
fix: optimize agentic chat prompt caching — 62% cost reduction (Based…