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.5.tar.gz (11.9 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.5-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: uselemma_tracing-3.0.5.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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.5.tar.gz
Algorithm Hash digest
SHA256 13b102fcaadf7b401ddb36265480033b1101204645c14587eb1d65efddb2888e
MD5 ea8b2cde1defef1764cab6f92fb7bd07
BLAKE2b-256 59f83e7cb27af075dcce5edfde8aa7c3deab11a140a306d28b7af836c0e0e839

See more details on using hashes here.

File details

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

File metadata

  • Download URL: uselemma_tracing-3.0.5-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e8dbfb7189bc1eb315b1af6c25aa78110161204599b708140fc05de7e7ca47f5
MD5 a59f0a7fdbacde13f84e3866c1f406d2
BLAKE2b-256 ae9353ad70b8be03550d92c376f02459b4debc027ba2131fb42ad51c21dab16a

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