Skip to main content

What Gets Tracked Automatically

LLM Calls

Every call to GPT-4, Claude, Gemini, etc. with token usage and cost.

Tool Executions

All tool calls with inputs, outputs, and timing.

Agent Reasoning

Chain-of-thought and decision reasoning.

Errors

Tool failures and LLM errors with context.

Installation

Basic Usage

Add SentrialCallbackHandler to your agent’s callbacks:

Handler Options

handler.set_input()

Set the user’s original query before running the agent. This is stored as the session’s input.

handler.finish()

Complete the session with all accumulated metrics. This is the recommended way to finalize a session — it automatically includes token counts, cost, and duration.
This replaces manually calling client.complete_session() with handler stats — finish() does it all in one call.

Accessing Usage Stats

After your agent runs, access real metrics from the handler:

Completing the Session

Use handler.finish() to complete the session with all accumulated metrics:
finish() automatically includes token counts, cost, duration, user input, and assistant output — no need to pass them manually.

Quick Setup with create_agent_with_sentrial()

For the simplest possible integration, use create_agent_with_sentrial(). It handles all LangChain version differences (including LangChain 1.0+ with LangGraph) automatically.
create_agent_with_sentrial() auto-detects your LangChain version. On LangChain 1.0+ it uses LangGraph’s create_react_agent. On older versions it uses AgentExecutor.

LangChain 1.0+ / LangGraph

LangChain 1.0+ deprecated AgentExecutor in favor of LangGraph. The Sentrial callback handler works with both. For LangGraph agents, pass the handler via config:
LangGraph’s create_react_agent never fires on_agent_finish, so handler.finish() uses the last LLM output as the assistant response automatically.

Full Production Example

Supported Models

Cost calculation is built-in for popular models:

Next Steps

Python SDK Reference

Full SDK documentation.

Sessions & Tracking

Deep dive into session tracking.