LangChain & LangGraph
The ANTS Platform CallbackHandler plugs into LangChain's callback system.
Pass it when you invoke a chain, model, agent or LangGraph graph, and each run
becomes an observation in one trace:
- Chains and graph nodes become chain or span observations.
- LLM and chat model calls become generations with model, messages, token usage and cost.
- Tools and retrievers become tool and retriever observations.
- Agents become agent observations.
- Errors are recorded with level
ERROR.
Set up
Set ANTS_PLATFORM_PUBLIC_KEY, ANTS_PLATFORM_SECRET_KEY and
ANTS_PLATFORM_HOST (see Python SDK).
CallbackHandler arguments: public_key (to pick a specific client when you
run several) and update_trace (default False; when True, the root chain's
name, input and output are also written to the trace).
Set user, session, tags and agent (Python)
In Python, pass trace attributes as LangChain run metadata with these keys:
| Metadata key | Sets |
|---|---|
ants_platform_user_id | Trace user |
ants_platform_session_id | Trace session |
ants_platform_tags | Trace tags (list of strings) |
ants_platform_agent_name | Agent name shown in the AI Agent Center |
Without ants_platform_agent_name, the agent name comes from an enclosing ANTS
Platform agent observation if there is one, otherwise from the root run's name.
To group LangChain calls under your own agent, run them inside
ants.start_as_current_observation(as_type="agent", name=..., agent_name=...).
LangGraph
Pass the handler in the graph's config. Each node run is traced under one trace.
The same works in JS/TS with await app.invoke(input, { callbacks: [handler] }).
Troubleshooting
- Nothing is traced. The handler must be in the
callbackslist of the call you run (config={"callbacks": [handler]}in Python,{ callbacks: [handler] }in JS/TS). In JS/TS, also check that the span processor is registered before the call. - No user or session in Python. Use the
ants_platform_metadata keys above. Plainsession_idoruser_idkeys are stored as metadata only. - Missing token counts. Token usage comes from the model provider's response. Some providers do not return usage for streamed responses.
- Short scripts lose data. Call
ants.flush()(Python) orforceFlush()on the span processor (JS/TS) before exit.