LangChain & LangGraph (Python)
Forward LangChain and LangGraph events to Iqrar via a single BaseCallbackHandler subclass — covers LLM lifecycle, tool calls, agent actions, and chain runs.
LangChain-Py and LangGraph-Py share the same callback API (langchain_core.callbacks.BaseCallbackHandler), so a single handler covers both.
Install
pip install "iqrar-agent[langchain]"
Wire it in
from iqrar import Iqrar
from iqrar.adapters.langchain import IqrarCallbackHandler
iqrar = Iqrar(
org="acme",
jurisdiction="EU",
endpoint="https://api.your-deploy.workers.dev",
capabilities=["consumer_chatbot"],
)
handler = IqrarCallbackHandler(iqrar)
# LangChain
result = chain.invoke(input, config={"callbacks": [handler]})
# LangGraph — same handler
result = graph.invoke(input, config={"callbacks": [handler]})
# Or globally
from langchain.callbacks.manager import set_default_callbacks
set_default_callbacks([handler])
What gets instrumented
| LangChain hook | Iqrar event |
|---|---|
on_llm_start / on_chat_model_start | agent.invocation.start |
on_llm_end | agent.invocation.end (with token usage) |
on_llm_error | agent.invocation.error |
on_tool_start | agent.tool.start |
on_tool_end | agent.tool.end |
on_tool_error | agent.tool.error |
on_agent_action | agent.step |
on_agent_finish | agent.finish |
Token usage is extracted from response.llm_output.token_usage when the underlying LLM provides it (OpenAI, Anthropic, most provider integrations).
See also
· · - The TS equivalent: