LangChain callbacks see every tool call: start, end and error. Pass a handler in the run config and it covers every tool in the graph, sub-agents included, without changing the tools.
The example uses create_agent from LangChain 1.x, which runs on LangGraph. The same handler works with a hand-built StateGraph: pass it in the config of invoke, stream or ainvoke.
Prerequisites
- A model provider key (the example uses OpenAI,
OPENAI_API_KEY; any chat model works). - Python 3.10+ and the Python Ovelin client from the Python guide.
1. Get an API key
- In Ovelin, open Settings → API keys.
- Under Ingest key, create a key and copy it. It is shown once; Ovelin keeps only a hash.
- Copy the Organization ID from the same page.
Put all three values in your environment, not in code:
export OVELIN_URL="https://your-ovelin-host" # http://localhost:3000 for a local instance
export OVELIN_ORG_ID="your-organization-id"
export OVELIN_API_KEY="agl_live_…"An organization key can report for any agent. For production, give each agent its own key under Settings → API keys → Agent keys: a leaked agent key can then only write as that one agent, and you can revoke it alone.
2. Install
pip install langchain langchain-openai requests3. Minimal working example
"""A LangChain agent (built on LangGraph) whose every tool call is recorded in Ovelin.
A LangChain callback handler sees each tool start, end and error. Pass it in
the run config and it applies to every tool in the graph, including tools of
sub-agents, without touching the tools themselves.
"""
import time
from typing import Any
from uuid import UUID
from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent
from ovelin import RUN_ID, log_event
AGENT = "research-agent"
@tool
def search_crm(query: str) -> str:
"""Search the CRM for accounts matching a query."""
return f"3 accounts match '{query}': Acme, Globex, Initech"
@tool
def draft_email(to: str) -> str:
"""Draft (not send) a follow-up email."""
return f"Draft saved for {to}"
class OvelinCallbackHandler(BaseCallbackHandler):
"""Records each tool call once it finishes, with its outcome and duration."""
def __init__(self, agent: str) -> None:
self.agent = agent
self.calls: dict[UUID, tuple[str, Any, float]] = {}
def on_tool_start(self, serialized: dict[str, Any], input_str: str, *, run_id: UUID, inputs=None, **kw) -> None:
self.calls[run_id] = (serialized.get("name", "tool"), inputs or input_str, time.monotonic())
def on_tool_end(self, output: Any, *, run_id: UUID, **kw) -> None:
self._record(run_id, "success")
def on_tool_error(self, error: BaseException, *, run_id: UUID, **kw) -> None:
self._record(run_id, "failure", error=str(error))
def _record(self, run_id: UUID, outcome: str, **extra: Any) -> None:
name, args, started = self.calls.pop(run_id, ("tool", None, time.monotonic()))
log_event(
self.agent,
name,
outcome=outcome,
data_accessed=["customer_pii"],
metadata={"tool_input": args, "duration_ms": round((time.monotonic() - started) * 1000), **extra},
)
agent = create_agent(ChatOpenAI(model="gpt-5.1"), tools=[search_crm, draft_email])
result = agent.invoke(
{"messages": [("user", "Find accounts with overdue invoices and draft a follow-up to billing@example.com.")]},
config={"callbacks": [OvelinCallbackHandler(AGENT)]},
)
print(result["messages"][-1].content)
print(f"Recorded in Ovelin under run {RUN_ID}")4. Run it
cd examples/python
python langchain_agent.py5. Check it in Ovelin
- Open Activity. The newest rows are the events you just sent, each with a sequence number and a seal.
- Click one. The detail panel shows the payload as received; Recompute seal checks its seal again.
- One event per tool call:
search_crm, thendraft_email. Open Agents: an agent name Ovelin had not seen before is registered automatically, without an owner. Give it one so someone is accountable for it.
To confirm nothing was altered later, open Integrity. It recomputes every seal and every link between records, and reports Trail intact.
Common errors
| Response | Cause | Fix |
|---|---|---|
401 Invalid API key | The key is wrong, revoked, or belongs to another organization. | Create a key in Settings → API keys and check OVELIN_ORG_ID is the same organization's. |
401 Missing API key (use Authorization: Bearer <key>) | The Authorization header was not sent. | Send Authorization: Bearer $OVELIN_API_KEY. The key never goes in the body. |
422 Payload must include a 'source' field | The JSON has no source. | Add "source": "custom" (or n8n, make_com, openai_api, zapier). |
422 Invalid option: expected one of "success"|"failure"|"unknown" | outcome has another value, such as warning. | Use success, failure or unknown. |
400 Invalid organization id | OVELIN_ORG_ID is not the organization's UUID. | Copy the Organization ID from Settings → API keys. |
429 Rate limit exceeded | More than 600 events a minute for one organization. | Retry after the Retry-After header. To send many events, use the batch endpoint (up to 100 per request). |
| No events | The handler was passed to the model, not the run. | Put it in config={"callbacks": [...]} on invoke, so it reaches the tool calls. |
How this guide is tested
Every code block above is copied from examples/python/langchain_agent.py by node scripts/sync-guide-snippets.mjs, and pnpm examples:test runs it against a local Ovelin and checks the events were sealed. The model is replaced by a local scripted stand-in (examples/_testing/scripted-model.mjs) that calls the tools in a fixed order, so the test needs no provider key; with a real key, unset ANTHROPIC_BASE_URL/OPENAI_BASE_URL and the same code talks to the provider.

