Skip to main content
Use Lemma’s OpenAI Agents integration when your app uses the OpenAI Agents SDK trace processor system. The integration creates one Lemma trace for each OpenAI Agents trace, maps model calls to generations, maps function calls to tools, and preserves parent IDs so child spans stay nested correctly. You do not need to wrap agent runs in lemma.trace(). Register the processor once, then run OpenAI Agents normally. A complete docs-chat agent using this integration lives in examples/openai-agents (TypeScript) and examples/python/openai-agents (Python). See Runnable examples.

TypeScript

Install both packages:
Register the Lemma processor before running agents. By default, credentials are read from LEMMA_API_KEY and LEMMA_PROJECT_ID:
You can also pass credentials and identity options directly:
Then run your agent normally:

Python

Install the Python extra:
Register the Lemma processor before running agents. By default, credentials are read from LEMMA_API_KEY and LEMMA_PROJECT_ID:
You can also pass credentials and identity options directly:
Then run your agent normally:

Root input, output, user, and thread

Both TypeScript and Python processors promote the same root fields on the Lemma trace: Soft tool failures (isError / is_error / Mastra error: true) mark the tool span as ERROR without failing the root when a later generation succeeds. In Python, a model call that a timeout or cancellation interrupts is recorded as failed: its generation gets status ERROR and the name of the exception that ended it, such as CancelledError, and keeps its model and input. This does not mark the root as failed.

Flush and shutdown

In TypeScript, forceFlush() returns once Lemma has sent every trace that already ended. A trace ends when run() returns, when a streamed run’s result.completed resolves, or when a withTrace() block returns. A trace that hasn’t ended stays open, so when concurrent requests share one processor, one request’s flush doesn’t cut short another’s trace. shutdown() also sends the traces still open, once each: call it before the process exits.
In TypeScript, on a serverless or edge runtime that stops once the response is sent, such as Cloudflare Workers, Next.js, or AWS Lambda, call forceFlush() where Flush a framework integration shows. The Agents SDK calls each processor’s onTraceEnd in registration order and waits for each before calling the next. Register Lemma’s processor before any processor whose onTraceEnd waits on a timer or I/O: call addTraceProcessor(processor) before you add the other one, or pass setTraceProcessors([processor, other]). Otherwise the forceFlush() after await result.completed can return before that trace’s delivery starts; the next forceFlush() or shutdown() waits for it. In Python, force_flush() and shutdown() both send any still-open traces exactly once. In both languages, a later onTraceEnd / on_trace_end for a trace already sent is a no-op, so the append-only ingest store never gets a span twice.
In async Python code, the processor hands each finished trace to a worker pool, so retries don’t block your event loop. Await aforce_flush() before a short-lived process such as a serverless handler returns: it waits without blocking the loop. aforce_flush() and force_flush() wait up to 34 seconds for traces still queued, then drop the rest with one warning. Once 1,000 traces are waiting, the pool drops each new one with a warning. In sync Python code, Runner.run_sync() waits up to 34 seconds for its trace’s delivery before it returns. An AWS Lambda handler or an RQ (Redis Queue) job that calls it doesn’t need to flush. The SDK drops a trace that hasn’t started sending within those 34 seconds and logs a warning; one already sending continues in the background. While Lemma is unreachable, each call takes up to 34 seconds longer.

What Lemma records

OpenAI Agents trace group_id is recorded as the Lemma thread id, so runs in the same conversation stay grouped.

What gets recorded

The integration always records prompts, tool inputs, tool outputs, model output, and error messages. A trace cannot show what a run produced or why it failed without them. Redact secrets and sensitive user data before they reach the agent.

Debug this integration

Use debug mode to confirm spans are arriving live and nested under the expected parent:
Look for span started and span ended logs. Tool/function spans should include the generation or agent span ID as parentId in TypeScript or parent_id in Python.