RAG Context Tracking
Retrieval Augmented Generation (RAGs) is a common way to augment the generation of your LLM by retrieving a set of documents based on the user query and giving it to the LLM to use as context for answering, either by using a vector database, getting responses from an API, or integrated agent files and memory.
It can be challenging, however, to build a good quality RAG pipeline, making sure the right data was retrieved, preventing the LLM from hallucinating, monitor which documents are the most used and keep iterating to improve it, this is where integrating with LangWatch can help, by integrating your RAG you unlock a series of Guardrails, Measurements and Analytics for RAGs LangWatch.
To capture a RAG span, you can use the @langwatch.span(type="rag")
decorator, along with a call to .update()
to add the contexts
to the span:
If you have document or chunk ids from the results, we recommend you can to capture them along with the id using RAGChunk
, as this allows them to be grouped together and generate documents analytics on LangWatch dashboard:
Then you’ll be able to see the captured contexts that will also be used later on for evaluatios on LangWatch dashboard:
To capture a RAG span, you can use the @langwatch.span(type="rag")
decorator, along with a call to .update()
to add the contexts
to the span:
If you have document or chunk ids from the results, we recommend you can to capture them along with the id using RAGChunk
, as this allows them to be grouped together and generate documents analytics on LangWatch dashboard:
Then you’ll be able to see the captured contexts that will also be used later on for evaluatios on LangWatch dashboard:
When using LangChain, generally your RAG happens by calling a Retriever
.
We provide a utility langwatch.langchain.capture_rag_from_retriever
to capture the documents found by the retriever and convert it into a LangWatch compatible format for tracking. For that you need to pass the retriever as first argument, and then a function to map each document to a RAGChunk
, like in the example below:
Alternatively, if you don’t use retrievers, but still want to capture the context for example from a tool call that you do, we also provide a utility langwatch.langchain.capture_rag_from_tool
to capture RAG contexts around a tool. For that you need to pass the tool as first argument, and then a function to map the tool’s output to RAGChunk
s, like in the example below:
Then you’ll be able to see the captured contexts that will also be used later on for evaluatios on LangWatch dashboard:
To capture a RAG, you can simply start a RAG span inside the trace, giving it the input query being used:
Then, after doing the retrieval, you can end the RAG span with the contexts that were retrieved and will be used by the LLM:
On LangChain.js, RAG spans are captured automatically by the LangWatch callback when using LangChain Retrievers, with source
as the documentId.
To track the RAG context when using the REST API, add a new span of type rag
, you may also refer the LLM generation as the child of it:
Was this page helpful?