Skip to main content

Objective

Start with a working LangGraph agent and put Orq.ai behind it: route its model calls through the AI Gateway, ground its answers in a Knowledge Base, and record each run in Traces. The agent logic itself barely changes.

Use Case

Reach for this pattern when:
  • An agent is already built on LangChain or LangGraph.
  • Standing up a vector database and juggling provider API keys is not worth the overhead.
  • Seeing what the agent actually did at runtime matters for debugging.

Prerequisites

Step 1: Install and set up the SDK

Install Orq.ai alongside LangGraph and the LangChain packages.

Step 2: Turn on tracing

Tracing is one call. orq_tracing_setup hooks into LangChain’s callback system, so every agent run, tool call, and model response streams to Orq.ai with no further changes to the agent.
Python

Step 3: Create a LangGraph agent with the router

Point a standard LangChain ChatOpenAI model at the Orq.ai router by overriding base_url. From there the agent is ordinary LangGraph: create_agent is LangChain’s prebuilt constructor that compiles a LangGraph agent under the hood, here wired with one example tool.
Python

Step 4: Test the agent

A small helper wraps the agent call so the later steps stay short. The agent picks the tool, runs it, and returns the answer.
Python
Python
The agent responds:

Step 5: Switch models through the router

The router addresses models with a provider/model string, so switching providers is a one-line change. Nothing else moves: the agent, tools, and prompt all stay as they were.
Python
The same agent now answers through a different provider:

Step 6: Set up the Knowledge Base

Ground the agent in real documents with a Knowledge Base. This takes three calls: create the Knowledge Base, upload the source file, then create a datasource that chunks and indexes it. Create the Knowledge Base and keep its id to reference it later.
Python
Upload the source document. The file is sent as base64-encoded content.
Python
Create a datasource to chunk and embed the file. Chunking runs asynchronously, so poll the datasource until its status is completed. To tune chunk size and overlap, see Chunking Strategy.
Python
The poll reports each status until indexing completes:

Step 7: Add the Knowledge Base search tool

Expose the Knowledge Base to the agent as a tool. search_policy runs a retrieval query and returns the matching chunks, and the system prompt forces the agent to call it before answering.
Python

Step 8: Ask a grounded question

Ask something that can only be answered from the uploaded policy. The agent calls search_policy first, then answers from the chunks it retrieves.
Python
The agent answers from the policy document:

Step 9: Check the Traces

Open AI Studio > Observability > Traces to inspect any run: the user message, the tool calls, the retrieved chunks, the model responses, and the timings. The setup from Step 2 already captures all of it, with nothing else to add.
The traces viewed in the AI Gateway of calling the created agent.

Traces of the LangGraph agent call

To learn more about Traces see Traces.
The LangGraph agent now runs on Orq.ai for routing, retrieval, and observability. Swap the model, the Knowledge Base, or the prompt without rewriting the agent loop.

Simple RAG

Build the same retrieval flow as a standalone deployment, without a framework.

Advanced RAG

Layer more retrieval techniques on top of a Knowledge Base.

LangGraph framework reference

AI Gateway and OpenTelemetry observability details for LangGraph.