Skip to main content
Retrieval Augmented Generation (RAG) is a common pattern in LLM applications where you first retrieve relevant context from a knowledge base and then use that context to generate a response. LangWatch provides specific ways to capture RAG data, enabling better observability and evaluation of your RAG pipelines. By capturing the contexts (retrieved documents) used by the LLM, you unlock several benefits in LangWatch:
  • Specialized RAG evaluators (e.g., Faithfulness, Context Relevancy).
  • Analytics on document usage (e.g., which documents are retrieved most often, which ones lead to better responses).
  • Deeper insights into the retrieval step of your pipeline.
There are two main ways to capture RAG spans: manually creating a RAG span or using framework-specific integrations like the one for LangChain.

Manual RAG Span Creation

You can manually create a RAG span by using tracer.withActiveSpan() with type: "rag". Inside this span, you should perform the retrieval and then update the span with the retrieved contexts. The contexts should be a list of LangWatchSpanRAGContext objects. The LangWatchSpanRAGContext object allows you to provide more metadata about each retrieved chunk, such as document_id, chunk_id, and content. Here’s an example:
In this example:
  1. performRAG uses tracer.withActiveSpan() with type: "rag".
  2. Inside performRAG, we simulate a retrieval step.
  3. span.setRAGContexts(ragContexts) is called to explicitly log the retrieved documents.
  4. The generation step (generateAnswerFromContext) is called, which itself can be another span (e.g., an LLM span).

Advanced RAG Patterns

Multiple Retrieval Sources

You can capture RAG contexts from multiple sources in a single span:

RAG with Metadata

You can include additional metadata in your RAG contexts:

Error Handling

When working with RAG operations, it’s important to handle errors gracefully and capture error information in your spans:

Best Practices

  1. Use Descriptive Span Names: Name your RAG spans clearly to identify the retrieval method or source.
  2. Include Metadata: Add relevant attributes like retrieval method, thresholds, or source information.
  3. Handle Errors Gracefully: Wrap RAG operations in try-catch blocks and capture error information.
  4. Optimize Context Size: Be mindful of the size of context content to avoid performance issues.
  5. Use Consistent Document IDs: Use consistent naming conventions for document and chunk IDs.
  6. Control Data Capture: Use data capture configuration to manage what gets captured in sensitive operations.
By effectively capturing RAG spans, you gain much richer data in LangWatch, enabling more powerful analysis and evaluation of your RAG systems. Refer to the SDK examples for more detailed implementations. For more advanced RAG patterns and framework-specific implementations:
For production RAG applications, combine manual RAG spans with Semantic Conventions for consistent observability and better analytics.