🛡️ AgentFlare Docs

LangChain Integration

Drop-in callback handler for LangChain and LangGraph agents.

Overview

If your agent uses LangChain or LangGraph, AgentFlare integrates with zero changes to your existing chain logic. Just add guard.callback to the callbacks list.

AgentFlare implements BaseCallbackHandler from langchain-core. It hooks into the on_llm_end and on_tool_start lifecycle events, extracting token counts and model names automatically.


Setup

pip install agentflare langchain-core
from agentflare import AgentFlare
 
guard = AgentFlare(
    api_key="ag_...",
    agent_id="langchain-agent",
    cost_threshold=5.0,
)

Usage

With a chain

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
 
model = ChatOpenAI(model="gpt-4o")
prompt = ChatPromptTemplate.from_template("Answer: {question}")
chain = prompt | model
 
# Add AgentFlare callback — no other changes needed
result = chain.with_config(callbacks=[guard.callback]).invoke(
    {"question": "What is the capital of France?"}
)

With LangGraph

from langgraph.graph import StateGraph
 
graph = StateGraph(...)
# ... build your graph ...
app = graph.compile()
 
# Inject the callback at invocation time
result = app.invoke(
    {"messages": [("user", "Run the analysis")]},
    config={"callbacks": [guard.callback]},
)

What gets tracked automatically

EventWhat firesWhat's extracted
LLM callon_llm_endmodel, input_tokens, output_tokens
Tool callon_tool_starttool_name

Token counts are read from the generation_info dict returned by the LLM. AgentFlare handles different key names across providers:

ProviderInput tokens keyOutput tokens key
OpenAIprompt_tokenscompletion_tokens
Anthropicinput_tokensoutput_tokens
Google Geminiprompt_token_countcandidates_token_count

If a key is missing, the count defaults to 0 (no cost is recorded for that call, but the event is still saved).


Handling pause mid-chain

When an agent is paused, the next send_event call inside the callback returns False and caches _paused = True. However, LangChain callbacks don't interrupt a running chain — the current LLM call will complete.

If you need hard stop behavior, check guard.is_paused between chain steps:

for step in steps:
    if guard.is_paused:
        print("Agent paused — stopping")
        break
    result = chain.with_config(callbacks=[guard.callback]).invoke(step)

Without LangChain installed

If langchain-core is not installed, importing AgentFlareCallback raises an ImportError with a clear message. The rest of the SDK (AgentFlare, AgentEvent) works fine without LangChain.

On this page