Docs/Integrations/Langchain

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

bash
pip install "ants-platform[langchain]" langchain-openai

Set ANTS_PLATFORM_PUBLIC_KEY, ANTS_PLATFORM_SECRET_KEY and ANTS_PLATFORM_HOST (see Python SDK).

python
from ants_platform import AntsPlatform from ants_platform.langchain import CallbackHandler from langchain_openai import ChatOpenAI ants = AntsPlatform() handler = CallbackHandler() llm = ChatOpenAI(model="gpt-4o-mini") response = llm.invoke("What is AI?", config={"callbacks": [handler]}) ants.flush()

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 keySets
ants_platform_user_idTrace user
ants_platform_session_idTrace session
ants_platform_tagsTrace tags (list of strings)
ants_platform_agent_nameAgent name shown in the AI Agent Center
python
chain.invoke( {"input": question}, config={ "callbacks": [handler], "metadata": { "ants_platform_user_id": "user-123", "ants_platform_session_id": "session-abc", "ants_platform_tags": ["support"], "ants_platform_agent_name": "support-agent", }, }, )

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.

python
from ants_platform.langchain import CallbackHandler handler = CallbackHandler() app = workflow.compile() result = app.invoke( {"topic": "AI agents"}, config={"callbacks": [handler]}, )

The same works in JS/TS with await app.invoke(input, { callbacks: [handler] }).

Troubleshooting

  • Nothing is traced. The handler must be in the callbacks list 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. Plain session_id or user_id keys 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) or forceFlush() on the span processor (JS/TS) before exit.
© 2026 ANTS Platform, Inc.Docs v1.0 · Last updated June 2026