This directory contains Python examples demonstrating how to use the tracenet package for comprehensive tracing and observability of AI agents and language model applications.
- Python 3.8 or higher
- OpenAI API key
- Langfuse instance (local or cloud)
- Clone the repository and navigate to the examples directory:
git clone https://github.com/stackgenhq/tracenet
cd tracenet/examples/python-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
cp env.example .env # Edit .env with your actual API keys and configuration
Create a .env file with the following variables:
# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key_here
# Langfuse Configuration for Observability
LANGFUSE_PUBLIC_KEY=your_langfuse_public_key_here
LANGFUSE_SECRET_KEY=your_langfuse_secret_key_here
LANGFUSE_HOST=http://localhost:3000
# Agent Configuration
AGENT_NAME=creative_story_agent
TRACENET_TRACER=langfuse
TRACENET_SERVICE_NAME=agent_serviceA simple example demonstrating basic tracing functionality:
python hello-world.pyThis will:
- Initialize the tracing system
- Create a simple agent that generates a creative story
- Demonstrate automatic and manual tracing
- Show how to use spans and custom attributes
The example demonstrates several key concepts:
- Automatic Framework Detection
import tracenet```
2. **Manual Tracing**
```python
from tracenet import trace, start_span, set_agent_name
@trace(name="generate_story")
def generate_story(prompt):
with start_span("story_generation") as span:
# Your code here
span.update(output=result)- Agent Configuration
# Set via environment variable
AGENT_NAME=creative_story_agent
# Or programmatically
from tracenet import set_agent_name
set_agent_name('creative_story_agent')| Variable | Description | Default |
|---|---|---|
TRACENET_TRACER |
Tracing backend to use | langfuse |
TRACENET_SERVICE_NAME |
Service name for traces | agent_service |
AGENT_NAME |
Agent identifier for grouping traces | None |
- Agent Names: Always set an agent name either via environment variable or programmatically:
from tracenet import set_agent_name
set_agent_name('my_agent_name')-
Error Handling: The middleware automatically handles errors and updates spans appropriately.
-
Cleanup: For long-running applications, flush traces periodically:
from tracenet import flush
flush()-
Import Error:
ModuleNotFoundError: No module named 'tracenet'Solution: Install the package:
pip install tracenet
-
No Traces Appearing: Check that your Langfuse API keys are correct and the host is accessible.
-
Missing Agent Name: Set the agent name via environment variable or programmatically.