Silvanexumdocs

LangChain

Meter and attribute a LangChain agent's spend by routing its model calls through the Silvanexum gateway.

LangChain is one of the most common ways to build an agent. Silvanexum captures its spend with zero rewrite: point LangChain's model client at the Silvanexum gateway and every call your chain makes is metered on your own provider key, attributed, and counted toward your budgets.

The gateway is OpenAI-compatible (/gateway/openai/v1), so use langchain-openai and an OpenAI model, and make sure your org has an OpenAI provider key saved (Settings → Keys). Native Anthropic traffic isn't gateway-proxied yet. Your key needs the run scope.

Install

pip install silvanexum langchain langchain-openai

Route the model through the gateway

import os
from silvanexum import Silvanexum
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
 
sx = Silvanexum()  # reads SILVANEXUM_API_KEY
 
llm = ChatOpenAI(
    model="gpt-4.1",
    base_url=sx.gateway.openai_base_url(),          # https://api.silvanexum.com/gateway/openai/v1
    api_key=os.environ["SILVANEXUM_API_KEY"],        # your Silvanexum key, not an OpenAI key
    default_headers={"x-silvanexum-trace": "notes-summary"},  # optional: stitch a multi-hop trace
)
 
prompt = ChatPromptTemplate.from_messages([
    ("system", "You summarize research notes into three bullet points."),
    ("human", "{notes}"),
])
chain = prompt | llm
 
notes = "Long-context models reduce retrieval complexity but raise cost..."
print(chain.invoke({"notes": notes}).content)

Every model call the chain makes — including tool-calling turns — now flows through the gateway on your BYO OpenAI key, with no duplicate run.

See the spend

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

Or open the Spend and Budgets views in the dashboard (or the VS Code extension) — gateway spend counts toward your org budgets and, with a block-mode budget, an over-cap call is rejected with 402.

Next steps

On this page