Optimizing LLM Token Usage with Production Monitoring in Natural Language to SQL System
dataherald · Tech · 2023 · source Agentic: no · promotional bias: low · source-backed · extracted with prompt 0.5.6 (schema v2)
Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.
Problem
Domain: Natural-language-to-SQL production monitoring
Dataherald needed to control LLM token costs in a production RAG agent after manual monitoring failed to catch runaway few-shot retriever context growth.
Constraints:
- The production agent used LangChain and expensive GPT-4-32K-class models.
- Manual TikToken and MongoDB monitoring missed a critical bug.
- Few-shot retriever token usage grew uncapped over time.
- The issue reached about 150,000 tokens per query.
Architecture
Dataherald implemented LangSmith production monitoring, identified the uncapped retriever within hours, and fixed token usage down to about 25,500 tokens per query.
Diagram confidence: reconstructed (reconstructed from prose).
flowchart LR langchain-rag-agent["LangChain RAG Agent"] few-shot-retriever["Few-Shot Retriever"] langsmith-monitoring["LangSmith Monitoring"] llm-query-generator["LLM Query Generator"] langchain-rag-agent --> few-shot-retriever langchain-rag-agent --> langsmith-monitoring few-shot-retriever --> llm-query-generator langsmith-monitoring --> langchain-rag-agent
Components
| Component | Role | Interacts with |
|---|---|---|
LangChain RAG Agent |
Generates SQL from natural-language questions using retrieved context. | Few-Shot Retriever, LangSmith Monitoring |
Few-Shot Retriever |
Adds examples to prompt context, including the runaway token source. | LLM Query Generator |
LangSmith Monitoring |
Traces production token usage and component behavior. | LangChain RAG Agent |
LLM Query Generator |
Produces SQL using retrieved prompt context. | — |
Data flow
Production LangChain requests flow through LangSmith monitoring, token usage is traced per component, the few-shot retriever bug is identified, and fixes reduce the amount of context sent to the LLM.
Why this architecture
Component-level production observability was needed because aggregate/manual token checks did not reveal which chain component was growing unbounded.
Models used: LangChain · LangSmith · GPT-4-32K
Scale
- Token usage reached about 150,000 tokens per query before the fix.“approximately 150,000 tokens per query.”summary, stated
- Token usage fell to about 25,500 tokens per query.“approximately 25,500 tokens per query—an 83% cost reduction.”summary, stated
- The fix produced an 83% cost reduction.“an 83% cost reduction.”summary, stated
Alternatives considered (roads not taken)
Manual TikToken and MongoDB monitoring
Rejected because: It failed to identify the uncapped token-growth bug.
Patterns
observability-tracing (operations)
LangSmith traced production token usage and exposed the runaway retriever.
cost-aware-routing (cost-control)
The fix reduced tokens and cost by 83%.
query-rewriting (text-to-sql)
The production system is a natural-language-to-SQL RAG agent.
Tradeoffs
Adopt production observability tooling
Chose LangSmith monitoring over Manual TikToken and MongoDB checks.
Rationale: LangSmith found the critical token bug within hours.
Gotchas (what bit them)
Few-shot retrieval can silently grow token usage without bounds.
Root cause: The retriever accumulated too much prompt context over time.
Resolution: Use production tracing to identify and cap/fix token growth.
Evaluation
Dataherald measured tokens per query and cost reduction before and after observability-driven fixes.
Metrics: Tokens per query · Cost reduction · Detection time
Outcomes
- The token bug was identified within hours.
- Tokens fell from about 150,000 to about 25,500 per query.
- Costs fell 83%.
The case centers on per-query token usage for a production natural-language-to-SQL RAG agent.