Skip to main content

Agent Observability Python Framework Module: LangChain

agento11y-langchain provides callback handlers that map LangChain lifecycle events into agento11y generation recorder lifecycles.

Installation

pip install agento11y agento11y-langchain
pip install langchain-openai

Usage

from agento11y import Client
from agento11y_langchain import with_agento11y_langchain_callbacks

client = Client()
config = with_agento11y_langchain_callbacks(None, client=client, provider_resolver="auto")

End-to-end example (invoke + stream)

from langchain_openai import ChatOpenAI
from agento11y import Client
from agento11y_langchain import Agento11yLangChainHandler, with_agento11y_langchain_callbacks

client = Client()
handler = Agento11yLangChainHandler(
    client=client,
    provider_resolver="auto",
    agent_name="langchain-example",
    agent_version="1.0.0",
)

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

# Non-stream call -> SYNC generation mode.
result = llm.invoke(
    "Summarize why retry budgets matter.",
    config=with_agento11y_langchain_callbacks(None, client=client, provider_resolver="auto"),
)
print(result.content)

# Stream call -> STREAM generation mode + TTFT tracking.
for chunk in llm.stream(
    "Give me three short reliability tips.",
    config=with_agento11y_langchain_callbacks(None, client=client, provider_resolver="auto"),
):
    if chunk.content:
        print(chunk.content, end="", flush=True)
print()

# Advanced usage: explicit handler wiring remains supported.
_ = llm.invoke("manual handler wiring", config={"callbacks": [handler]})

client.shutdown()

Conversation grouping

The handler resolves the conversation id per invocation, in this order:

  1. conversation_id / session_id / group_id in the callback metadata, invocation params, or configurable
  2. A thread_id in the same places
  3. The handler's conversation_id constructor argument
  4. A synthetic per-run id

Pass conversation_id on the constructor when your application owns the conversation identity. Per-invocation identity still wins, so a handler built once per process cannot override it.

handler = Agento11yLangChainHandler(
    client=client,
    agent_name="my-chain",
    conversation_id=request.conversation_id,
)

Without any of these, each run becomes its own conversation.

With the default callback span capture, model and tool spans follow the chain's callback parent IDs, including when there is no active application span. Separate invocations start separate traces; keep the conversation ID stable to group turns. To put multiple invocations in one trace, run them under the same application span. Tool execution spans carry gen_ai.tool.call.id when LangChain supplies it. Streaming content blocks and completed AIMessageChunk outputs are supported.

Behavior

  • Lifecycle mapping:
    • on_llm_start / on_chat_model_start -> generation recorder
    • System and developer messages passed to on_chat_model_start are lifted out of the input message list into system_prompt (joined with a blank line when there are several), since the wire format has no system role. An explicit invocation_params["system_prompt"] wins.
    • on_tool_start / on_tool_end / on_tool_error -> start_tool_execution
    • on_chain_start / on_chain_end / on_chain_error -> framework chain spans
    • on_retriever_start / on_retriever_end / on_retriever_error -> framework retriever spans
    • A chain or retriever span that ends with an error leaves the error text off under METADATA_ONLY and FULL_WITH_METADATA_SPANS (which resolves to FULL with experimental OTel generation export), and when a generation_sanitizer is configured. Its status then reads sdk_error.
    • on_llm_new_token -> first-token timestamp for stream mode
  • Mode mapping: non-stream -> SYNC, stream -> STREAM.
  • Provider resolver parity:
    • explicit provider metadata when available
    • model-name inference (gpt-/o1/o3/o4 -> openai, claude- -> anthropic, gemini- -> gemini)
    • fallback -> custom
  • Framework tags/metadata are always set:
    • agento11y.framework.name=langchain
    • agento11y.framework.source=handler
    • agento11y.framework.language=python
    • metadata["agento11y.framework.run_id"]=<run id>
    • metadata["agento11y.framework.thread_id"]=<thread id> (when present in callback metadata/config)
    • metadata["agento11y.framework.parent_run_id"] (when available)
    • metadata["agento11y.framework.component_name"] (serialized component identity)
    • metadata["agento11y.framework.run_type"] (llm, chat, tool, chain, retriever)
    • metadata["agento11y.framework.tags"] (normalized callback tags)
    • metadata["agento11y.framework.retry_attempt"] (when available)
    • generation span attributes mirror low-cardinality framework metadata keys

Call client.shutdown() during teardown to flush buffered telemetry.

Metadata

Release files for agento11y-langchain 0.23.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agento11y-langchain 0.23.0
File Size Uploaded Provenance Actions
agento11y_langchain-0.23.0.tar.gz 14.3 kB With provenance Details for agento11y_langchain-0.23.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agento11y-langchain 0.23.0
File Interpreter ABI Platform Actions
agento11y_langchain-0.23.0-py3-none-any.whl Python 3 none any Details for agento11y_langchain-0.23.0-py3-none-any.whl

Total release size: 20.5 kB

Release files / agento11y_langchain-0.23.0.tar.gz

Download URL agento11y_langchain-0.23.0.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d137b2d108814724a135e39add8c4d05ac24384f886e2ee131f83ca1358326c1
BLAKE2b-256 checksum
How to use checksums
9d0d737071bd48977e26b38a6ce5913c32845f29a72e0796f8b0cbdda1fd7111
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Release files / agento11y_langchain-0.23.0-py3-none-any.whl

Download URL agento11y_langchain-0.23.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
665e6821dbb8a88e94d68c060374fee6d3b866fb9ee2f8d48887eaf11ad837ad
BLAKE2b-256 checksum
How to use checksums
02c4b1e87bf6d33691b7093539ce78ce5edb574e2f1d95fe7b84242ae1474313
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Security for agento11y-langchain 0.23.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for agento11y_langchain-0.23.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page