Getting Started

Use LangDB’s Python SDK to generate completions, monitor API usage, retrieve analytics, and evaluate LLM workflows efficiently.

PyPI version License: MIT

Key Features

LangDB exposes two complementary capabilities:

  1. Chat Completions Client – Call LLMs using the LangDb Python client. This works as a drop-in replacement for openai.ChatCompletion while adding automatic usage, cost and latency reporting.

  2. Agent Tracing – Instrument your existing AI framework (ADK, LangChain, CrewAI, etc.) with a single init() call. All calls are routed through the LangDB collector and are enriched with additional metadata regarding the framework is visible on the LangDB dashboard.


Quick Start (Chat Completions)

pip install pylangdb[client]
from pylangdb.client import LangDb

# Initialize LangDB client
client = LangDb(api_key="your_api_key", project_id="your_project_id")

# Simple chat completion
resp = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(resp.choices[0].message.content)

Agent Tracing Quick Start

Note: Always initialize LangDB before importing any framework-specific classes to ensure proper instrumentation.

Example Trace Screenshot

Google ADK Trace Example

Supported Frameworks (Tracing)

Framework
Installation
Import Pattern
Key Features

Google ADK

pip install pylangdb[adk]

from pylangdb.adk import init

Automatic sub-agent discovery

OpenAI

pip install pylangdb[openai]

from pylangdb.openai import init

Custom model provider support and Run Tracing

LangChain

pip install pylangdb[langchain]

from pylangdb.langchain import init

Automatic chain tracing

CrewAI

pip install pylangdb[crewai]

from pylangdb.crewai import init

Multi-agent crew tracing

Agno

pip install pylangdb[agno]

from pylangdb.agno import init

Tool usage tracing, model interactions

How It Works

LangDB uses intelligent monkey patching to instrument your AI frameworks at runtime:

Click to see technical details for each framework

Google ADK

  • Patches Agent.__init__ to inject callbacks

  • Tracks agent hierarchies and tool usage

  • Maintains thread context across invocations

OpenAI

  • Intercepts HTTP requests via AsyncOpenAI.post

  • Propagates trace context via headers

  • Correlates spans across agent interactions

LangChain

  • Modifies httpx.Client.send for request tracing

  • Automatically tracks chains and agents

  • Injects trace headers into all requests

CrewAI

  • Intercepts litellm.completion for LLM calls

  • Tracks crew members and task delegation

  • Propagates context through LiteLLM headers

Agno

  • Patches LangDB.invoke and client parameters

  • Traces workflows and model interactions

  • Maintains consistent session context

Installation

Configuration

Set your credentials (or pass them directly to the init() function):

Client Usage (Chat Completions)

Initialize LangDb Client

Chat Completions

Thread Operations

Get Messages

Retrieve messages from a specific thread:

Get Thread Cost

Get cost and token usage information for a thread:

Analytics

Get analytics data for specific tags:

Evaluate Multiple Threads

List Available Models

Framework-Specific Examples (Tracing)

Google ADK

OpenAI

LangChain

CrewAI

Agno

Advanced Configuration

Environment Variables

Variable
Description
Default

LANGDB_API_KEY

Your LangDB API key

Required

LANGDB_PROJECT_ID

Your LangDB project ID

Required

LANGDB_API_BASE_URL

LangDB API base URL

https://api.us-east-1.langdb.ai

LANGDB_TRACING_BASE_URL

Tracing collector endpoint

https://api.us-east-1.langdb.ai:4317

LANGDB_TRACING

Enable/disable tracing

true

LANGDB_TRACING_EXPORTERS

Comma-separated list of exporters

otlp, console

Custom Configuration

All init() functions accept the same optional parameters:

Technical Details

Session and Thread Management

  • Thread ID: Maintains consistent session identifiers across agent calls

  • Run ID: Unique identifier for each execution trace

  • Invocation Tracking: Tracks the sequence of agent invocations

  • State Persistence: Maintains context across callbacks and sub-agent interactions

Distributed Tracing

  • OpenTelemetry Integration: Uses OpenTelemetry for standardized tracing

  • Attribute Propagation: Automatically propagates LangDB-specific attributes

  • Span Correlation: Links related spans across different agents and frameworks

  • Custom Exporters: Supports multiple export formats (OTLP, Console)

API Reference

Initialization Functions

Each framework has a simple init() function that handles all necessary setup:

  • langdb.adk.init(): Patches Google ADK Agent class with LangDB callbacks

  • langdb.openai.init(): Initializes OpenAI agents tracing

  • langdb.langchain.init(): Initializes LangChain tracing

  • langdb.crewai.init(): Initializes CrewAI tracing

  • langdb.agno.init(): Initializes Agno tracing

All init functions accept optional parameters for custom configuration (collector_endpoint, api_key, project_id)

Troubleshooting

Common Issues

  1. Missing API Key: Ensure LANGDB_API_KEY and LANGDB_PROJECT_ID are set

  2. Tracing Not Working: Check that initialization functions are called before creating agents

  3. Network Issues: Verify collector endpoint is accessible

  4. Framework Conflicts: Initialize LangDB integration before other instrumentation

Last updated

Was this helpful?