Silvanexumdocs

LangGraph

Meter and attribute a stateful LangGraph agent's spend through the Silvanexum gateway.

LangGraph builds stateful, branching agents. Silvanexum captures their spend without touching your graph: point each node's model client at the Silvanexum gateway, and every model call across the graph is metered on your own key, attributed, and budgeted. Pass a shared trace header so a multi-node run stitches into one attributed trace.

The gateway is OpenAI-compatible (/gateway/openai/v1) — use langchain-openai and an OpenAI model, with an OpenAI provider key saved (Settings → Keys). Your key needs the run scope.

Install

pip install silvanexum langgraph langchain-openai

Point every node at the gateway

import os
from silvanexum import Silvanexum
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, MessagesState, START
 
sx = Silvanexum()  # reads SILVANEXUM_API_KEY
 
llm = ChatOpenAI(
    model="gpt-4.1",
    base_url=sx.gateway.openai_base_url(),
    api_key=os.environ["SILVANEXUM_API_KEY"],
    default_headers={"x-silvanexum-trace": "triage-graph"},  # shared across nodes
)
 
def respond(state: MessagesState):
    return {"messages": [llm.invoke(state["messages"])]}
 
graph = StateGraph(MessagesState)
graph.add_node("respond", respond)
graph.add_edge(START, "respond")
app = graph.compile()
 
app.invoke({"messages": [("human", "Triage this incident report...")]})

Every node's model call is metered on your BYO key — no duplicate run, no rewrite of your graph logic.

See the spend

print(sx.gateway.usage().totals)   # 30-day totals + recent calls

Gateway spend counts toward your org budgets; a block-mode budget rejects an over-cap call with 402 before it bills.

Next steps

On this page