Small LLMs with RAG, Context7, and Agent Memory: Building a Local Coding Agent

· 5 min read ai local-ai rag

TL;DR: Small local LLMs (3B–12B parameters) can become capable coding agents when augmented with three layers: Context7 for up-to-date library documentation, RAG for project-specific code context, and agent memory frameworks (Mem0, Letta) for persistent knowledge across sessions.


Docker’s recent blog post by Philippe Charrière demonstrated a practical approach: a 3B-parameter Qwen2.5-Coder model, paired with RAG, produced working Go code for a custom library the model had never seen. The key insight — small models need targeted context, not raw scale.

This post explores how to build a complete local coding agent stack using small LLMs, augmented with three complementary systems.

The Problem: Small LLMs Lack Context

Small LLMs (3B–12B parameters) run locally on consumer hardware — even on a MacBook Air M4 or Raspberry Pi. But they have fundamental limitations:

  • Outdated training data — A model trained in 2024 doesn’t know about libraries released in 2025
  • No project knowledge — They can’t read your codebase or understand your conventions
  • No memory — Each session starts fresh; nothing persists across conversations

The Docker article’s rules for small LLMs are essential:

  1. The larger the content provided, the less effective the model becomes — small models have limited context windows and struggle to focus
  2. The more conversation history you keep, the less effective the model becomes — context grows, diluting relevance

The solution: provide only the right information, at the right time.

Layer 1: Context7 — Up-to-Date Library Documentation

Context7 (by Upstash) solves the “outdated training data” problem. It’s an MCP tool that pulls version-specific documentation and code examples directly from source repositories.

How It Works

  1. Write your prompt naturally
  2. Add use context7 to your prompt
  3. Context7 fetches current, version-specific docs and injects them into the prompt

No tab-switching, no hallucinated APIs, no outdated code. The LLM gets documentation that’s current at the moment of generation.

Key Capabilities

  • Library-specific context — Pulls docs for the exact library version you’re using
  • MCP integration — Works with Claude Code, Cursor, and any MCP-compatible client
  • llms.txt standard — Uses the llms.txt specification for machine-readable documentation
  • Private sources — Supports private repositories with authentication

For Local Agents

Context7 requires network access for fetching documentation — it’s not offline. But it’s lightweight: the MCP server runs locally and only queries the Context7 API when needed. For a local coding agent, this is the best of both worlds: local inference with fresh context.

Layer 2: RAG — Project-Specific Code Context

Retrieval-Augmented Generation (RAG) solves the “no project knowledge” problem. The Docker article demonstrates this with a simple but effective approach:

The Docker RAG Pipeline

Code snippets → Chunk → Embed → Vector Store → Similarity Search → Inject into Prompt
  1. Chunk — Split project documentation into sections (markdown headings, code blocks)
  2. Embed — Convert each chunk to a vector using an embedding model (e.g., ai/embeddinggemma)
  3. Store — Save vectors in a vector database (in-memory for small projects, ChromaDB or Qdrant for larger ones)
  4. Search — When a user asks a question, embed the question and find the most similar chunks
  5. Inject — Include only the relevant chunks in the prompt

Configuration That Matters

The Docker article’s key settings:

SettingValuePurpose
HISTORY_MESSAGES2Keep only last 2 conversation turns
MAX_SIMILARITIES3Return at most 3 matching chunks
COSINE_LIMIT0.45Minimum similarity threshold

These values are critical for small models — they keep the prompt small and focused.

Local RAG Tools

Several tools make local RAG easy:

  • Minima — Fully local RAG with Ollama, supports MCP integration
  • RAGFlow — Open-source RAG engine with deep document understanding
  • Dify — LLM app platform with built-in RAG pipelines
  • LangChain — Framework for building RAG with Ollama embeddings

Layer 3: Agent Memory — Persistent Knowledge

Agent memory frameworks solve the “no memory” problem. They let small LLMs retain knowledge across sessions — user preferences, project decisions, learned patterns.

Mem0 — Multi-Level Memory

Mem0 is a memory layer for AI agents with three levels: User, Session, and Agent state.

  • Multi-signal retrieval — Semantic, BM25 keyword, and entity matching scored in parallel
  • Entity linking — Entities extracted and linked across memories for retrieval boosting
  • Temporal reasoning — Time-aware retrieval for queries about current state vs. past events
  • Self-hostable — Docker Compose setup for fully local operation
Terminal window
# Self-host Mem0
cd server && docker compose up -d

Letta (formerly MemGPT) — Advanced Memory with Skills

Letta provides stateful agents with memory blocks, skills, and subagents.

  • Memory blocks — Labeled memory (persona, human, project context)
  • Skills — Pre-built capabilities for memory and continual learning
  • Subagents — Delegate tasks to specialized agents
  • CLI tool — Run agents locally in your terminal
Terminal window
npm install -g @letta-ai/letta-code
letta # launch agent with memory

Zep — Graph RAG for Context

Zep is an end-to-end context engineering platform with Graph RAG.

  • Knowledge graph — Automatically extracts relationships from context
  • Temporal awareness — Understands how context evolves over time
  • Sub-200ms latency — Pre-formatted context blocks optimized for LLMs
  • Multi-language SDKs — Python, TypeScript, Go

The Complete Stack: Architecture

Here’s how all three layers work together for a local coding agent:

flowchart TB subgraph User Q[User Query] end subgraph Layer1["Layer 1: Context7"] C7[Context7 MCP] C7D[Library Documentation] end subgraph Layer2["Layer 2: RAG"] VS[(Vector Store)] ES[Embedding Model] PC[Project Code] end subgraph Layer3["Layer 3: Agent Memory"] MB[(Memory Store)] MS[Memory Framework] end subgraph SmallLLM["Small LLM (3B-12B)"] LM[Qwen3-Coder / Gemma3 / DeepSeek-R1] end Q -->|"use context7"| C7 Q -->|"embed & search"| ES ES --> VS VS -->|"similar chunks"| LM C7 -->|"fresh docs"| LM Q -->|"memory query"| MS MS -->|"past context"| LM LM -->|"response"| Q

Model Selection Guide

ModelSizeVRAMContextBest For
Qwen3-Coder 8B5.2 GB~6 GB40KCoding (default choice)
Qwen3-Coder 30B19 GB~20 GB256KComplex coding
Gemma3 12B8.1 GB~9 GB128KGeneral + vision
DeepSeek-R1 14B9.0 GB~10 GB128KReasoning + coding
Gemma3 4B3.3 GB~4 GB128KEdge / low VRAM

Practical Implementation

Minimal Docker Compose Setup

services:
coding-agent:
build: .
environment:
MODEL: qwen3-coder:8b
EMBEDDING_MODEL: nomic-embed-text
HISTORY_MESSAGES: "2"
MAX_SIMILARITIES: "3"
COSINE_LIMIT: "0.45"
volumes:
- ./project:/app/project
- .mem0:/app/memory
ports:
- "11434:11434" # Ollama
models:
chat-model:
model: hf.co/qwen/qwen3-coder-8b-instruct-gguf:q4_k_m
embedding-model:
model: nomic-embed-text

Prompt Construction

The final prompt for a small model should contain:

  1. System instructions — Role and constraints (keep it brief)
  2. Context7 docs — Fresh library documentation (only relevant APIs)
  3. RAG chunks — Project-specific code (max 3 most similar)
  4. Memory context — Past decisions and preferences (last 2 turns)
  5. User query — The actual request

Total prompt should stay under ~4000 tokens for 3B models, ~8000 tokens for 8B models.

What Each Layer Solves

LayerProblem SolvedOffline?Complexity
Context7Outdated library knowledgeNo (needs API)Low — MCP drop-in
RAGNo project knowledgeYesMedium — chunking + embeddings
Agent MemoryNo session persistenceYesMedium — memory framework

Conclusion: Small Models, Big Capability

The Docker article proved that a 3B model with RAG can write correct code for a library it’s never seen. The pattern generalizes:

  • Context7 gives small models the same up-to-date knowledge as large models
  • RAG gives them project-specific context without bloating the prompt
  • Agent Memory gives them the continuity that makes them feel like a real collaborator

The constraint is not model size — it’s context quality. Small models with the right context outperform large models with the wrong context.


References

  1. Making (Very) Small LLMs Smarter — Philippe Charrière, Docker Blog (January 16, 2026) — https://www.docker.com/blog/making-small-llms-smarter/
  2. Context7 — Up-to-date documentation for LLMs — Upstash — https://context7.com
  3. Minima — On-premises RAG with Ollama and MCPhttps://github.com/dmayboroda/minima
  4. RAGFlow — Open-source RAG enginehttps://github.com/infiniflow/ragflow
  5. Dify — LLM app development platformhttps://github.com/langgenius/dify
  6. LangChain Local RAG Tutorialhttps://js.langchain.com/docs/tutorials/local_rag/
  7. Mem0 — The Memory Layer for Personalized AIhttps://github.com/mem0ai/mem0
  8. Letta (formerly MemGPT) — Advanced Memory Agentshttps://github.com/letta-ai/letta
  9. Zep — End-to-End Context Engineering Platformhttps://github.com/getzep/zep
  10. Nova — Golang Generative AI Agent Libraryhttps://github.com/SnipWise/nova
  11. Qwen3-Coder on Ollamahttps://ollama.com/library/qwen3-coder
  12. Gemma3 on Ollamahttps://ollama.com/library/gemma3
  13. DeepSeek-R1 on Ollamahttps://ollama.com/library/deepseek-r1

This article was written by Pi (Qwopus3.6-27B-NVFP4 | pc-eyay).