Skip to main content

tencentcloud-agentobs-sdk-langchain

中文文档

Observability SDK for LangChain / LangGraph: automatically intercepts LangChain callback and LangGraph node lifecycle events, converts them into OTel spans conforming to the Tencent Cloud CLS GenAI Trace specification, and uploads directly to Tencent Cloud CLS.

  • Zero intrusion: one setup() call (or LangChainInstrumentor().instrument()) completes instrumentation — no changes to business code
  • Faithful trace tree: each graph.invoke() produces a complete trace with an entry → agent → step → chat / tool hierarchy that mirrors the framework's real call structure
  • Concurrency safe: parent-child relationships are derived from the framework's parent_run_id — parallel tool calls and nested sub-agents just work
  • Compliance ready: three content capture modes (full / truncate / off) for strict data-residency requirements
  • Full metrics: token usage, finish_reason, tool error classification, ReAct round tracking

Installation

pip install tencentcloud-agentobs-sdk-langchain

Runtime dependencies are installed automatically. The key ones:

  • langchain-core > 0.1.0 — the target framework being instrumented
  • tencentcloud-cls-sdk-python >= 1.0.8 — CLS upload client

For LangGraph agents you additionally need langgraph, and for LLM calls a provider integration such as langchain-openai.

Requires Python >= 3.9.


Quick Start

A single setup() handles all initialization:

import os
from tencentcloud_agentobs_sdk_langchain import setup, CLSConfig

# 1. Configure your LLM provider (managed by LangChain, not this SDK)
os.environ["OPENAI_API_KEY"] = "sk-xxxx"

# 2. Enable CLS observability (must be called before running any agent)
# Option A: all from environment variables
setup()

# Option B: explicit CLSConfig (unset fields fall back to env vars)
setup(CLSConfig(
    endpoint="ap-guangzhou.cls.tencentcs.com",
    topic_id="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    secret_id="your_secret_id",
    secret_key="your_secret_key",
))

# 3. Use LangChain / LangGraph as usual
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

@tool
def get_weather(city: str) -> str:
    """Look up the weather for a city."""
    return {"Beijing": "Sunny, 26C"}.get(city, "Sunny, 25C")

agent = create_react_agent(ChatOpenAI(model="gpt-4o-mini"), tools=[get_weather])
result = agent.invoke({"messages": [("user", "What's the weather in Beijing?")]})
print(result["messages"][-1].content)

setup() creates a TracerProvider, attaches the CLSCloudExporter, registers instrumentation, and returns the provider (useful if you need to attach additional processors).

Manual wiring (advanced)

from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from tencentcloud_agentobs_sdk_langchain import LangChainInstrumentor
from tencentcloud_agentobs_sdk_langchain.cls_cloud_exporter import CLSCloudExporter

provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(CLSCloudExporter()))
LangChainInstrumentor().instrument(tracer_provider=provider)

Configuration

All fields resolve with the priority: explicit CLSConfig value > environment variable > built-in default.

Field Env var Default Meaning
endpoint CLS_ENDPOINT CLS API endpoint (required)
topic_id CLS_TOPIC_ID CLS log topic ID (required)
secret_id CLS_SECRET_ID Tencent Cloud SecretId (required)
secret_key CLS_SECRET_KEY Tencent Cloud SecretKey (required)
service_name CLS_SERVICE_NAME / OTEL_SERVICE_NAME langchain-app Service / application name
content_mode OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT full full / truncate / off
batch_size CLS_BATCH_SIZE 32 Spans per upload batch (clamped 1–1000)
debug CLS_DEBUG false Verbose SDK logging
local_dump CLS_LOCAL_DUMP false Also write each uploaded batch to a local jsonl
local_dump_file CLS_LOCAL_DUMP_FILE cls_spans.jsonl Local dump file path
user_id CLS_USER_ID End-user ID written to gen_ai.user.id

SDK logs are written to cls_sdk.log (configurable via CLS_SDK_LOG_FILE / CLS_SDK_LOG_LEVEL), using a rotating file handler.


Span hierarchy

For a LangGraph create_react_agent, one invoke() produces:

entry  enter_application
└ agent  invoke_agent
  ├ step  react round_1
  │  ├ chain  call_model
  │  ├ chain  RunnableSequence
  │  │  ├ chain  Prompt
  │  │  └ chat   <model>
  │  ├ chain  should_continue
  │  └ tool   execute_tool <name>        (one per parallel tool call)
  └ step  react round_2
     └ ...

The tree faithfully reflects LangChain's real callback nesting: chat is nested under its RunnableSequence, tool executions attach directly under the current step, and each entry into the LangGraph agent node opens a new ReAct step.


Examples

See example.py for a minimal end-to-end quick start (LangGraph ReAct agent + setup()).


License

Apache-2.0. See LICENSE.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tencentcloud_agentobs_sdk_langchain-0.0.1.tar.gz (50.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file tencentcloud_agentobs_sdk_langchain-0.0.1.tar.gz.

File metadata

File hashes

Hashes for tencentcloud_agentobs_sdk_langchain-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d05a464021fd90cc7633e9741c4eefa535fa0c1677f4f0a78a33f177e9511c96
MD5 2a848306e6f1406adb9d626094c7e06d
BLAKE2b-256 67ef6b9bbbe36cbfb1ed5cc6280ed333cb2d5f9a95c3f2e7f285c8d0736f20f0

See more details on using hashes here.

File details

Details for the file tencentcloud_agentobs_sdk_langchain-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tencentcloud_agentobs_sdk_langchain-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aaab2b20d73ecef4410781c1748c64c3d7d0a6fb5fe081b1d270c0b1b6008caa
MD5 64a35dd13fd5558433a40909656103e6
BLAKE2b-256 717a6d59d5d885bd03762d0a499f648f2628cd91e816ce7287e10ebc02aad631

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 files

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