-
Notifications
You must be signed in to change notification settings - Fork 1.1k
docs(examples): add beginner-friendly LLM tracing example #4481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| LLM Tracing 101 — A beginner-friendly guide to LLM observability with Traceloop. | ||
|
|
||
| This example shows how to: | ||
| 1. Initialize Traceloop | ||
| 2. Make an LLM call with OpenAI | ||
| 3. See the trace output in your console (no backend needed!) | ||
|
|
||
| Setup: | ||
| pip install openai opentelemetry-sdk opentelemetry-api traceloop-sdk | ||
| export OPENAI_API_KEY="sk-..." | ||
|
|
||
| Run: | ||
| python llm_tracing_101.py | ||
| """ | ||
|
|
||
| import os | ||
| from opentelemetry.sdk.trace.export import ConsoleSpanExporter | ||
| from traceloop.sdk import Traceloop | ||
|
|
||
|
|
||
| def main(): | ||
| # ------------------------------------------------------------------ | ||
| # Step 1: Initialize Traceloop with the ConsoleSpanExporter | ||
| # ------------------------------------------------------------------ | ||
| # The ConsoleSpanExporter prints traces to your terminal, so you can | ||
| # see exactly what Traceloop captures without configuring any | ||
| # external observability backend (like Jaeger, Datadog, or Honeycomb). | ||
| Traceloop.init( | ||
| app_name="llm-tracing-101", | ||
| exporter=ConsoleSpanExporter(), | ||
| ) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Step 2: Make an LLM call | ||
| # ------------------------------------------------------------------ | ||
| # Traceloop auto-instruments OpenAI, so every call you make will be | ||
| # traced automatically. No decorators or context managers needed. | ||
| from openai import OpenAI | ||
|
|
||
| client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) | ||
|
|
||
| print("Making LLM call...") | ||
|
|
||
| response = client.chat.completions.create( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '1,100p' examples/llm_tracing_101.py
printf '\n--- relevant guidance and examples ---\n'
find .. -name AGENTS.md -o -name CONTRIBUTING.md -o -name pyproject.toml | head -80
rg -n --glob '*.py' 'chat\.completions\.create|choices\[0\]|APIError|RateLimitError|timeout' examples packages/sample-app | head -160Repository: traceloop/openllmetry Length of output: 19545 🤖 get_repo_knowledge executed:
Length of output: 7627 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- dependency/version declarations ---'
rg -n --glob 'pyproject.toml' --glob 'requirements*.txt' --glob 'setup.cfg' --glob 'Pipfile*' --glob '*.md' 'openai|llm_tracing_101|beginner|example|uv run' . | head -220
printf '%s\n' '--- focused analogous examples ---'
for f in \
packages/sample-app/sample_app/openai_functions.py \
packages/sample-app/sample_app/azure_openai.py \
packages/sample-app/sample_app/openai_vision_base64_example.py \
packages/sample-app/sample_app/manual_logging_example.py \
packages/sample-app/sample_app/openai_streaming.py
do
if [ -f \"$f\" ]; then
echo \"--- $f ---\"
cat -n \"$f\" | sed -n '1,120p'
fi
done
printf '%s\n' '--- repository guidance files ---'
for f in CONTRIBUTING.md AGENTS.md; do
if [ -f \"$f\" ]; then
echo \"--- $f ---\"
rg -n -C 3 'uv|example|OpenAI|error|exception|validation|API' \"$f\" | head -160
fi
doneRepository: traceloop/openllmetry Length of output: 18030 🌐 Web query:
💡 Result: <search_synthesis> <source_evidence> Citations:
Handle failures from the OpenAI request.
Do not add a 🤖 Prompt for AI Agents |
||
| model="gpt-4o-mini", | ||
| messages=[ | ||
| {"role": "system", "content": "You are a helpful assistant."}, | ||
| {"role": "user", "content": "What is observability in 2 sentences?"}, | ||
| ], | ||
| max_tokens=100, | ||
| ) | ||
|
|
||
| print(f"\nResponse: {response.choices[0].message.content}") | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Step 3: View the trace | ||
| # ------------------------------------------------------------------ | ||
| # After the script finishes, you'll see a JSON trace printed to your | ||
| # terminal. Look for: | ||
| # - "name": "openai.chat" — the LLM call span | ||
| # - "attributes" — model, prompt tokens, completion tokens, etc. | ||
| # | ||
| # In production, swap ConsoleSpanExporter for an OTLP exporter to | ||
| # send traces to your observability backend of choice. | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use
uv runfor the install command.Replace
pip installwith the repository-requireduv runcommand format.As per coding guidelines: “Execute all package management commands through the uv package manager using 'uv run <command>'.”
🤖 Prompt for AI Agents
Source: Coding guidelines