Skip to content

Optimize agentic chat token usage — tool schemas burn 69% of input tokens #4676

Closed

Description

@beastoin

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

  1. 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.

  2. No cross-request memory — thread_id is 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.

  3. 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.

  4. 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).

  5. 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 guard
  • backend/utils/llm/chat.py:398-744 — _get_agentic_qa_prompt() system prompt
  • backend/utils/retrieval/tools/ — All 22 tool definitions
  • backend/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

Activity

  1. added
    intelligence
    p1
    maintainer
    on Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    intelligencemaintainerp1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions