LangGraph and LangChain

Trace LangGraph graphs and LangChain chains with cost per node, feature and customer, observing your Bedrock calls or routing them through the gateway.

Updated September 26, 2026

opentoken.init() instruments LangChain (which LangGraph builds on): graph runs, nodes, chains and every model call with tokens. Calls made through langchain-aws are also traced at the Bedrock level, and counted once.

1. Install

shell
pip install "getopentoken[langchain,bedrock]" langgraph langchain-aws

2. Environment

Create an ingest key under API keys (scope Ingest) and export it as OT_INGEST_KEY.

.env
# OT_INGEST_KEY: an ingest key from API keys (scope: Ingest). Keep it in your secret store.
export OTEL_EXPORTER_OTLP_ENDPOINT="https://getopentoken.com/api/otel"
export OTEL_SERVICE_NAME="support-agent"

3. Observe your graph

Your model calls keep going to Bedrock in your account:

agent.py
import opentoken

opentoken.init()

from langchain_aws import ChatBedrockConverse
from langgraph.graph import END, START, MessagesState, StateGraph

llm = ChatBedrockConverse(model="us.amazon.nova-lite-v1:0")


def answer(state: MessagesState):
    return {"messages": [llm.invoke(state["messages"])]}


graph = StateGraph(MessagesState)
graph.add_node("answer", answer)
graph.add_edge(START, "answer")
graph.add_edge("answer", END)
app = graph.compile()

with opentoken.context(feature="support", customer="acme"):
    result = app.invoke({"messages": [("user", "Summarize our refund policy in one sentence.")]})
print(result["messages"][-1].content)

Enforce budgets through the gateway (optional)

chat_model() returns a ChatOpenAI wired to the gateway with fail-open to Bedrock. Use it anywhere LangChain takes a chat model, including prebuilt LangGraph agents.

agent.py
import opentoken
from opentoken.adapters.langchain import chat_model
from langgraph.graph import END, START, MessagesState, StateGraph

opentoken.init(base_url="https://getopentoken.com/v1")  # OT_API_KEY for the gateway, OT_INGEST_KEY for traces

# ChatOpenAI wired to the gateway, with fail-open straight to Bedrock in your account if we're slow or down.
llm = chat_model("amazon/nova-lite")


def answer(state: MessagesState):
    return {"messages": [llm.invoke(state["messages"])]}


graph = StateGraph(MessagesState)
graph.add_node("answer", answer)
graph.add_edge(START, "answer")
graph.add_edge("answer", END)

with opentoken.context(feature="support"):
    result = graph.compile().invoke({"messages": [("user", "Say hello in one sentence.")]})
print(result["messages"][-1].content)

Don't point a plain ChatOpenAI at the gateway while init() is instrumenting: the gateway would measure the call and the span would report it again. chat_model() marks its spans so each call is counted once.