Skip to main content
wrap_llm() wraps LLM SDK clients directly. Every LLM generation call and tool use block in the response is automatically traced — no callback wiring needed.

When to use this

ScenarioUse
Agent built with LangChain/LangGraphLangChain callback
Agent using raw OpenAI/Anthropic/Gemini SDKwrap_llm() — see pages below
Agent using MCP sessions directlywrap()

Supported SDKs

Gemini SDK

google.genai.Client — new SDK with function calling

OpenAI SDK

openai.OpenAI / AsyncOpenAI — chat completions + tools

Anthropic SDK

anthropic.Anthropic / AsyncAnthropic — messages + tool_use

How it works

import langsight

ls = langsight.init()  # reads LANGSIGHT_URL from env
client = ls.wrap_llm(your_llm_client, agent_name="my-agent", session_id="sess-001")
# Use client exactly as before — all calls auto-traced
wrap_llm() auto-detects the SDK based on the client class:
ClassDetected SDK
openai.OpenAI, openai.AsyncOpenAIOpenAI
anthropic.Anthropic, anthropic.AsyncAnthropicAnthropic
google.genai.ClientGemini (new SDK)
google.generativeai.GenerativeModelGemini (legacy)
Anything elseReturned unchanged (fail-open)

What gets traced

For each generation call, LangSight emits:
  1. LLM generation span — model, tokens, latency, span_type="agent"
  2. Tool call spans (one per tool use) — tool name, arguments, parent_span_id linking to the LLM span

Combining with MCP tracing

wrap_llm() and wrap() work independently. Use both when your agent calls LLM APIs directly and also uses MCP servers:
client = ls.wrap_llm(genai.Client(), agent_name="my-agent", session_id=sid)
traced_mcp = ls.wrap(mcp_session, server_name="postgres-mcp", session_id=sid)
Both share the same session_id, so all spans appear in a single session trace.

PII redaction

ls = LangSightClient(url="http://localhost:8000", redact_payloads=True)
# Tool arguments from LLM responses are not captured