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.6.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.6-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: uselemma_tracing-3.0.6.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.6.tar.gz
Algorithm Hash digest
SHA256 5ab5061ca4781c090216da40bed6ade0292baa13deb437d9c2b98d92b9b9770f
MD5 6a8a4612fbebdd963df4ab6ce4f3ffa2
BLAKE2b-256 5dd5adc0756ce1396f813c665286b8b071693e797da390422e94b08c8e09ebd7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: uselemma_tracing-3.0.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 6ef574dff02782088a446eeb75d38b52c0c01a1aac88991065fe2885f943a51e
MD5 2076d7980afe4c1f933c16f8a3f8939b
BLAKE2b-256 da4d2b5d8bda76751fccaf7463d308d4de5ab70cef1faf65af76948278a3a863

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