Context for AI Agents: How to Cut Token Costs by 60–90% Without Losing Data Control
I'm Denis Shokhirev, Enterprise AI architect based in Erlangen, Germany. At DennisCraft AI Studio I deliver production AI systems for B2B clients in logistics, fintech, and industrial automation, using Claude, Supabase, n8n, Doppler, and self-hosted Postgres. Last quarter, a logistics client burned through €3,200 in token costs for a single agent in one month—because the context strategy was “just send everything to the LLM.” This is not sustainable in regulated, cost-sensitive environments. H
I'm Denis Shokhirev, Enterprise AI architect based in Erlangen, Germany. At DennisCraft AI Studio I deliver production AI systems for B2B clients in logistics, fintech, and industrial automation, using Claude, Supabase, n8n, Doppler, and self-hosted Postgres. Last quarter, a logistics client burned through €3,200 in token costs for a single agent in one month—because the context strategy was “just send everything to the LLM.” This is not sustainable in regulated, cost-sensitive environments.
How Token Costs Spiral Out of Control
Most teams start by stuffing every possibly relevant doc, instruction, or data blurb into the prompt or system message. In development, this is fine. In production, with hundreds of daily users, the cost curve explodes. I’ve seen over 1.8M tokens/day on a single agent—half of it redundant. The main sinks:
- Static instructions copied for every request
- Full document dumps in system prompts
- Knowledge base search returns pasted wholesale
- Duplicated context between workflow steps (especially in n8n chains)
Architecture Patterns That Reduce Token Spend by 60–90%
Below are the exact patterns I use in DACH production deployments. These are not theoretical; they have reduced token bills by 60–90% for real clients.
1. Incremental Context Assembly
Never send the entire user/agent history. Build the minimal viable context for each step. In n8n, I use custom nodes to filter and join only the crucial message fragments:
def build_context(messages, required_types):
context = []
for msg in reversed(messages):
if msg['type'] in required_types:
context.append(msg['content'])
if len(context) >= 4: # limit context depth
break
return "\n".join(reversed(context))
2. Embedding Search Instead of Full-Text Dumps
Replace full-document injection with embedding search (Supabase pgvector or self-hosted Postgres + pgvector). Only retrieve relevant paragraphs, not entire PDFs.
from supabase import create_client
supabase = create_client(url, key)
def search_knowledge(query, top_k=3):
embedding = get_embedding(query)
rows = supabase.table("knowledge").select("*").limit(top_k).eq("embedding", embedding).execute()
return [row['text'] for row in rows['data']]
3. Caching Prompt-Response Combinations
Don’t regenerate answers for identical prompts. Cache common prompt hashes and responses in Postgres or Supabase.
def get_or_generate_answer(prompt_hash):
answer = db.query("SELECT response FROM cache WHERE hash = %s", (prompt_hash,))
if answer:
return answer[0]
else:
response = call_llm_api(...)
db.execute("INSERT INTO cache (hash, response) VALUES (%s, %s)", (prompt_hash, response))
return response
Data Control: Why Cloud Vector DBs Don’t Cut It
If your clients are in DACH, fintech or regulated logistics, data control is not optional. I never use cloud vector DBs (Pinecone, Weaviate) if there’s any risk of data leaving the EU or being stored outside the client’s perimeter. Self-hosted Postgres with pgvector is the only stack that consistently passes audits.
| Solution | Data Control | Cost | Production Experience |
|---|---|---|---|
| Pinecone | Minimal | High | Demo |
| Weaviate Cloud | Minimal | Medium | Demo |
| Self-hosted Postgres + pgvector | Full | Low | Production |
Security: Static Analysis and Access Controls
Token savings are pointless if your agent leaks data. Every production agent I ship runs through semgrep and bandit to catch SQL injection and unsafe API calls (see semgrep.dev, 2024). Embedding index access is always behind service accounts with minimal roles (Supabase RLS or direct Postgres GRANT).
FAQ
Should I switch to self-hosted Postgres for data control?
If you serve DACH or fintech clients, you have no choice. Any cloud embedding store fails audits and can lead to regulatory fines.
Can you really cut token costs without degrading quality?
Yes, with the right architecture (incremental context, embedding search, caching). My agents routinely cut token bills by 65–80% after these patterns are implemented.
Which embedding search stack actually works in production?
Supabase with pgvector or self-hosted Postgres. Both are stable under load and pass compliance reviews.
How do you monitor for data leaks in AI agents?
Use static analysis (semgrep, bandit), log all embedding index queries, and set up anomaly-based alerts on access patterns.
What if the LLM still “asks” for more context?
Hard-limit history depth and embedding response size. Enforce these at the workflow layer (in n8n or orchestration code) to avoid “context bloat.”
Where in your LLM pipeline did token costs unexpectedly blow up? How much did it cost you before you fixed it? I run a free 30-min stack audit for DACH founders building AI in regulated markets. DM me on LinkedIn or write to @ger_dennis_ai.
Turn your process into an AI system
Fixed price. Production quality. DACH B2B focus.