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 stablemoda.conversation_id that groups multi-turn conversations together.
Installation
Quick Start
Initialization is synchronous in Python. Call
moda.init(...) once at startup before making LLM calls to ensure instrumentation is active.Prompt Management
Usemoda 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
Setting Conversation ID (Recommended)
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)
- 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: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:Verifying Data in Moda
After setting up, verify that data is flowing correctly:- Make a few LLM calls
- Call
moda.flush()to ensure data is sent - Check the Moda dashboard, conversations should appear within seconds
- 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_idinstead 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
- Call
moda.flush()before your program exits - Check that your API key is correct
- Verify network connectivity to the ingest endpoint
- Make sure you installed
moda-ai - Check that your Python version is 3.10 or higher