About Portfolio Cases Services Blog Contact 🎙 Talk to AI
EN DE RU
🎙 Talk to AI
October 16, 2026 · 3 min read

Context Window for AI Agents: Cutting Memory Costs and Speeding Up Task Chains

I’m Denis Shokhirev, Agentic AI Systems Architect based in Freiburg im Breisgau, Germany. At DennisCraft AI Studio, I ship multi-agent AI systems for DACH B2B clients (logistics, fintech, industrial automation) using Claude, Supabase, n8n, Doppler, and self-hosted Postgres. Last month, one of my production agents blew through an OpenAI monthly budget in three days—root cause: uncontrolled context window growth. Context Window: The Real Production Bottleneck Token window limits aren’t just a t

Denis Shokhirev
Denis Shokhirev
Agentic AI Systems Architect
Telegram LinkedIn

I’m Denis Shokhirev, Agentic AI Systems Architect based in Freiburg im Breisgau, Germany. At DennisCraft AI Studio, I ship multi-agent AI systems for DACH B2B clients (logistics, fintech, industrial automation) using Claude, Supabase, n8n, Doppler, and self-hosted Postgres. Last month, one of my production agents blew through an OpenAI monthly budget in three days—root cause: uncontrolled context window growth.

Context Window: The Real Production Bottleneck

Token window limits aren’t just a theoretical concern. In production, the context window is the main driver of both inference cost and latency, as well as error rates from context overflow. Claude 3 Opus (Anthropic, 2024) supports up to 200K tokens, but every large-window call is expensive and adds significant delay (Anthropic Docs, 2024).

Unchecked, agent workflows accumulate logs, irrelevant history, and past outputs—ballooning the context window. I’ve seen task chains grind to a halt as latency climbs and costs spike.

Memory Optimization Patterns for Real-World Agents

1. Aggressive Filtering & Deduplication Before the Prompt

Agents tend to dump everything into the prompt: logs, state, history, intermediate outputs. In a recent RAG pipeline on n8n, I measured a 4x cost increase over 24 hours, all due to prompt bloat. My fix: filter out duplicates and non-informative fragments before passing data to the LLM.


def prepare_context(messages):
    seen = set()
    filtered = []
    for msg in messages:
        key = hash(msg['content'])
        if key not in seen and len(msg['content']) > 32:
            filtered.append(msg)
            seen.add(key)
    return filtered[-60:]  # Only the last 60 relevant messages

This approach consistently cuts context size by 40–60% across multiple agent deployments.

2. Tokenizer-Aware Chunking: Never Exceed Window Size

Anthropic and OpenAI both provide token counting utilities (tiktoken). I always measure actual token count before sending a prompt. If you exceed the window, you risk 413 errors or silent truncation—both dangerous in prod.


import tiktoken

def count_tokens(messages, model="claude-3-opus"):
    enc = tiktoken.encoding_for_model(model)
    return sum(len(enc.encode(m['content'])) for m in messages)

def fit_to_window(messages, window_size):
    total = 0
    result = []
    for msg in reversed(messages):
        tokens = len(tiktoken.encoding_for_model("claude-3-opus").encode(msg['content']))
        if total + tokens > window_size:
            break
        result.insert(0, msg)
        total += tokens
    return result

With this pattern, agents just grab the last N messages that fit, ensuring no overflow or runaway costs.

3. External Long-Term Memory: Supabase/Postgres, Not the Prompt

Storing all history in the prompt is unsustainable. For long-term memory, I push key facts and summaries to Supabase/Postgres. Only a brief summary and the last 5–10 messages make it into the prompt. This pattern reduces per-task costs by 60–80% in real-world workflows.


import psycopg2

def get_recent_summaries(agent_id):
    conn = psycopg2.connect("dbname=ai_prod user=agent password=secret")
    cur = conn.cursor()
    cur.execute("SELECT summary FROM memories WHERE agent_id=%s ORDER BY ts DESC LIMIT 3", (agent_id,))
    return [row[0] for row in cur.fetchall()]

RAG-style retrieval from Postgres delivers minimal latency (down to 1.2s per chain) and lowest token spend.

Comparing Context Management Strategies

MethodToken SavingsLatency (sec)Manageability
Raw prompt (no filter)0%3.0+Low
Filtering & deduplication40–60%2.1Medium
Tokenizer-aware chunking50–70%1.8High
Supabase/Postgres memory60–80%1.2Max

Context Window Pattern in Task Chains

In multi-agent n8n + Claude pipelines, long chains often break down: agents forget goals, repeat questions, or get stuck in log loops. My fix: explicit context window control at every step. Example n8n workflow:


- name: Fetch recent messages
  type: postgres
  params: {limit: 20}
- name: Summarize
  type: openai
  params: {prompt: "Summarize last 20 messages"}
- name: Build context window
  type: python
  params: {function: prepare_context}
- name: LLM decision
  type: anthropic
  params: {model: "claude-3-opus"}

This keeps context size predictable and the pipeline stable—even after a week of continuous operation.

FAQ

What is the optimal context window size?

For most agent tasks, 4–8K tokens per prompt suffices. Larger windows sharply increase costs and latency.

How should I store long-term agent memory?

I persist key facts and summaries in Supabase/Postgres. Only brief selections are injected into the prompt.

How do you monitor token spend?

I parse OpenAI/Anthropic usage logs daily and generate reports per agent. Outliers are easy to spot.

What if the window gets overfilled?

Filter, chunk, and drop noisy history (logs, metadata). Don’t try to squeeze in everything—summaries and key facts work best.

When should you expand the window?

Only if the agent’s task truly requires full history (e.g., multi-day negotiations or complex document threads).

Where do you see the most context window failures in production: memory management, LLM latency, or DB retrieval? I’d genuinely like to know.
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.

Continue reading
Anthropic Replaces Git: When AI Agents Break Traditional Dev Infrastructure
Open-Source Agent Workspaces: How Paperclip and ENZO Let You Run Agents on Your Own Keys and Infrastructure
Half Your Context Window Is Wasted: How MCP Dumps, Bloated Configs, and Unused Skills Make Every Claude Code Session More Expensive
How to connect any AI model to 1200+ business integrations in 5 minutes (MCP, CLI, API)
All articles →
Where this is applied
Services — what we build
Talk to the voice agent
Case studies
Ready to build?

Turn your process into an AI system

Production quality. DACH B2B focus.

Start a project → ← All articles