Skip to main content

RAG Agents Pattern

Retrieval-Augmented Generation (RAG) is the pattern of combining LLMs with external knowledge sources. StateBase makes RAG agents stateful and reliable by managing both conversation context and retrieved knowledge.

The Problem with Stateless RAG

Traditional RAG implementations lose context between requests:
The issue: The agent doesn’t remember what “it” refers to.

Stateful RAG with StateBase

StateBase tracks conversation history and retrieved documents:

Query Expansion

Use conversation history to improve retrieval:

Document Caching

Avoid re-retrieving the same documents:

Combine vector search with keyword search for better retrieval:

Citation Tracking

Track which documents were used to generate each answer:

Multi-Hop Retrieval

For complex questions, retrieve in multiple steps:

Confidence Scoring

Detect when the agent doesn’t have enough information:

Conversation Summarization

Periodically summarize long conversations to keep context manageable:

Complete RAG Agent Example


Best Practices

✅ Do This

  • Expand queries with conversation context (resolve pronouns)
  • Cache retrieved documents (avoid redundant searches)
  • Track citations (know which docs were used)
  • Use hybrid search (vector + keyword)
  • Monitor confidence scores (detect when you don’t know)

❌ Avoid This

  • Don’t ignore conversation history (leads to irrelevant retrieval)
  • Don’t retrieve too many docs (context overflow)
  • Don’t trust retrieval blindly (validate relevance)
  • Don’t forget to cite sources (transparency matters)

Next Steps


Key Takeaway: RAG without state is like having a librarian with amnesia. StateBase makes your RAG agents remember context and improve over time.