Complete the session with all accumulated metrics. This is the recommended way to finalize a session — it automatically includes token counts, cost, and duration.
handler.finish( success=True, # Whether the agent succeeded failure_reason=None, # Reason if success=False custom_metrics={"quality": 4.5} # Optional custom metrics)
This replaces manually calling client.complete_session() with handler stats — finish() does it all in one call.
After your agent runs, access real metrics from the handler:
# Token countshandler.total_prompt_tokens # Input tokens across all LLM callshandler.total_completion_tokens # Output tokens across all LLM callshandler.total_tokens # Total tokens used# Cost (calculated from actual usage)handler.total_cost # Total cost in USD# Call countshandler.llm_calls # Number of LLM API calls# Durationhandler.duration_ms # Total duration in milliseconds# Model infohandler.model_name # Model used (e.g., "gpt-4o")# Get everything as a dictsummary = handler.get_usage_summary()# {# "llm_calls": 3,# "total_prompt_tokens": 2500,# "total_completion_tokens": 800,# "total_tokens": 3300,# "total_cost": 0.0425,# "model": "gpt-4o",# "duration_ms": 4521# }
For the simplest possible integration, use create_agent_with_sentrial(). It handles all LangChain version differences (including LangChain 1.0+ with LangGraph) automatically.
from sentrial import SentrialClientfrom sentrial.langchain import create_agent_with_sentrialfrom langchain_openai import ChatOpenAIclient = SentrialClient(api_key="sentrial_live_xxx")llm = ChatOpenAI(model="gpt-4o")agent = create_agent_with_sentrial( llm=llm, tools=[search_tool, calculator_tool], client=client, agent_name="my-agent", user_id="user-123",)# One call — session creation, tracking, and completion are all automaticresult = agent("What's the weather in San Francisco?")
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+ deprecated AgentExecutor in favor of LangGraph. The Sentrial callback handler works with both. For LangGraph agents, pass the handler via config: