Skip to main content

Overview

The Moda Python SDK provides automatic instrumentation for your LLM applications with built-in conversation threading. Every LLM call is automatically tracked with a stable moda.conversation_id that groups multi-turn conversations together.

Installation

This installs the core SDK along with instrumentation for OpenAI and Anthropic. For other providers, install the corresponding instrumentation package:

Quick Start

Initialization is synchronous in Python. Call moda.init(...) once at startup before making LLM calls to ensure instrumentation is active.

Prompt Management

Use moda prompts sync to version prompt files, then render them through the SDK:
moda.prompt(...).render(...) attaches prompt metadata to OpenTelemetry spans, including moda.prompt_key, moda.prompt_id, moda.prompt_version, and moda.prompt_version_id. See Prompt Management for the code-first sync workflow.

Conversation Tracking

For production use, explicitly set a conversation ID to group related LLM calls:

Setting User ID

Associate LLM calls with specific users for per-user analytics:

Scoped Context Managers

For scoped context (useful in request handlers):

Direct Setters

For cases where context managers aren’t suitable (e.g., setting once at request start):

Reading Current Context

Computing Conversation ID

You can manually compute a conversation ID from messages:

Automatic Fallback

If you don’t set a conversation ID, the SDK automatically computes a stable one based on:
  • The first user message in the conversation
  • The system prompt (if present)
This works well for prototyping and simple use cases:
For production applications, explicit conversation IDs are recommended as they provide:
  • Predictable grouping regardless of message content
  • Integration with your existing session/thread identifiers
  • Easier debugging and correlation with your application logs

Configuration

Environment Variables

Programmatic Configuration

Advanced Configuration

The SDK supports additional configuration options:

Filtering Instruments

Control which LLM providers are instrumented:
The Instruments enum supports a wide range of providers and frameworks:

Supported Providers

The SDK automatically instruments:

OpenRouter Support

OpenRouter provides access to multiple LLM providers through a unified API. Since OpenRouter uses an OpenAI-compatible interface, it works automatically with the Moda SDK:
OpenRouter model names use the format provider/model-name. See the OpenRouter models page for all available models.

Verifying Data in Moda

After setting up, verify that data is flowing correctly:
  1. Make a few LLM calls
  2. Call moda.flush() to ensure data is sent
  3. Check the Moda dashboard, conversations should appear within seconds
  4. Verify that multi-turn conversations share the same conversation ID

Data Captured

The SDK captures:

API Reference

Core Functions

Context Properties

Context Managers

Direct Functions

Troubleshooting

Conversation IDs not grouping correctly?
  • Use explicit moda.conversation_id instead of relying on auto-compute
  • If using auto-compute, ensure the first user message stays the same across turns
  • Check if system prompts are changing between calls
Data not appearing in Moda?
  • Call moda.flush() before your program exits
  • Check that your API key is correct
  • Verify network connectivity to the ingest endpoint
Import errors?
  • Make sure you installed moda-ai
  • Check that your Python version is 3.10 or higher