Skip to main content

OpenTelemetry-based tracing module for Lemma

Project description

uselemma-tracing

OpenTelemetry-based tracing for AI agents. Capture inputs, outputs, timing, token usage, and errors — then view everything in Lemma.

Installation

pip install uselemma-tracing

Quick Start

1. Register the tracer provider

Call register_otel once when your application starts. It reads LEMMA_API_KEY and LEMMA_PROJECT_ID from environment variables by default.

from uselemma_tracing import register_otel

register_otel()

2. Wrap your agent

agent creates a root OpenTelemetry span named ai.agent.run. Return a value from the wrapped function — the wrapper auto-captures it as ai.agent.output and closes the span automatically. The result is a TraceResult with named fields — and also supports tuple unpacking for backward compatibility.

from uselemma_tracing import TraceContext, agent

async def run_agent(user_message: str, ctx: TraceContext) -> str:
    result = await do_work(user_message)
    return result  # wrapper auto-captures output and closes the span

myAgent = agent("my-agent", run_agent)

# Named access (recommended)
res = await myAgent("hello", {"thread_id": "thread_123"})
print(res.result, res.run_id)

# Tuple unpacking also works
result, run_id, span = await myAgent("hello")

3. Add typed decorators (optional)

Use @tool, @retrieval, @llm, and @trace to add child spans to internal functions:

from uselemma_tracing import agent, tool, retrieval, TraceContext

@retrieval("vector-search")
async def search(query: str) -> list:
    return await vector_db.search(query, top_k=5)

@tool("lookup-order")
async def lookup_order(order_id: str) -> dict:
    return await db.orders.get(order_id)

async def run_agent(user_message: str, ctx: TraceContext) -> str:
    docs = await search(user_message)      # retrieval.vector-search span
    context = "\n".join(docs)
    return await generate_answer(context, user_message)

agent = agent("rag-agent", run_agent)

4. Context manager (no function extraction needed)

from uselemma_tracing import agent

async def handle_request(user_message: str) -> str:
    async with agent("my-agent", input=user_message) as run:
        response = await call_llm(user_message)
        run.complete(response)  # sets ai.agent.output; span closes on block exit
    return response

Export Behavior

  • Spans are exported in run-specific batches keyed by lemma.run_id.
  • A run batch is exported when its top-level ai.agent.run span ends.
  • force_flush() exports remaining runs in separate batches per run.

Environment Variables

Variable Description
LEMMA_API_KEY Your Lemma API key
LEMMA_PROJECT_ID Your Lemma project ID

Both are required unless passed explicitly to register_otel().

Documentation

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uselemma_tracing-3.0.3.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uselemma_tracing-3.0.3-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file uselemma_tracing-3.0.3.tar.gz.

File metadata

  • Download URL: uselemma_tracing-3.0.3.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.5 {"installer":{"name":"uv","version":"0.11.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for uselemma_tracing-3.0.3.tar.gz
Algorithm Hash digest
SHA256 4ae0c9b61be4219c402bb56f065d5ea64bde5087b4b6e46c79a294eef7c3402e
MD5 c9b73deeff43d6b7adde167e42ef58f9
BLAKE2b-256 6c926771014fcf2dd9016c57ccbaf915f60d609c30f53e3072616611eb4b4199

See more details on using hashes here.

File details

Details for the file uselemma_tracing-3.0.3-py3-none-any.whl.

File metadata

  • Download URL: uselemma_tracing-3.0.3-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.5 {"installer":{"name":"uv","version":"0.11.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for uselemma_tracing-3.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d1a224ef236743ab2719c5c3eea6c8cec9f92ea21bb19c85a5aa1b8051b0d89a
MD5 2c092371b37b1e4fc5a2ffa9078c1355
BLAKE2b-256 7089f55942985d6d70ab8a3c0395907f60cc39080094ecaa4cc926fc5e04ac01

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page