RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /How-to
  5. /How to manage context overflow for very long conversations
HOW-TO · INF

How to manage context overflow for very long conversations

intermediate·15 min·By Fredoline Eruo
PREREQUISITES

Ollama or similar chat runtime

What this does

When a conversation exceeds the model's context window, older messages are lost. This guide covers sliding window, summarization, and selective truncation strategies.

Steps

  1. Implement sliding window truncation. Keep the system prompt and most recent messages, dropping the oldest turns.

    import tiktoken
    
    def trim_to_window(messages, max_tokens=4096, model="llama3.2"):
        enc = tiktoken.get_encoding("cl100k_base")
        token_counts = [len(enc.encode(m.get("content", ""))) for m in messages]
        total = sum(token_counts)
        while total > max_tokens and len(messages) > 2:
            # Remove oldest user/assistant pair (keep system prompt at index 0)
            removed = messages.pop(1)
            total -= token_counts.pop(1)
        return messages
    
  2. Use summarization as an alternative to dropping. When the window is full, summarize the oldest messages and replace them.

    def summarize_old_messages(messages, model="llama3.2"):
        old_content = "\n".join(m["content"] for m in messages[:-4] if m["role"] != "system")
        if len(old_content) < 200:
            return messages
        # Ask the model to summarize
        summary = requests.post("http://localhost:11434/api/generate",
            json={"model": model, "prompt": f"Summarize:\n{old_content}", "stream": False})
        summary_text = summary.json()["response"]
        # Replace old messages with a single summary entry
        messages = [messages[0]] + [{"role": "user", "content": f"[Previous context summary]: {summary_text}"}] + messages[-4:]
        return messages
    
  3. Set num_ctx to the maximum supported value.

    curl -s http://localhost:11434/api/chat \
      -d '{"model": "llama3.2", "messages": [...], "options": {"num_ctx": 32768}}'
    
  4. Monitor token usage per turn.

    def count_tokens(text, model="llama3.2"):
        enc = tiktoken.get_encoding("cl100k_base")
        return len(enc.encode(text))
    

Verification

# Run a 50-turn conversation; verify no errors and the model recalls the last 5 turns
# Expected: Model remembers recent context without crashing or producing irrelevant output

Common failures

  • Dropping the system prompt: Always preserve messages[0] (system message) during truncation.
  • Summarization changes meaning: The summary may lose nuance. Keep the original last 3-4 turns intact.
  • tiktoken model mismatch: Use the correct encoding for your model. Llama models often use a custom tokenizer; install transformers and use AutoTokenizer.

Related guides

  • How to configure context window size for long documents
  • How to adjust context length when running on limited hardware
RELATED GUIDES
INF
How to configure context window size for long documents
INF
How to adjust context length when running on limited hardware
← All how-to guidesCourses →