Skip to main content
Use this when your agent pipeline makes multiple LLM calls in sequence — for example, an intent extraction step followed by a generation step. Each call gets its own child span. If you register the matching OpenInference instrumentor for your provider, each SDK call automatically produces its own child span with prompt, response, model, and token data. See Provider instrumentation.
import OpenAI from "openai";
import { registerOTel, wrapAgent } from "@uselemma/tracing";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { OpenAIInstrumentation } from "@arizeai/openinference-instrumentation-openai";

const provider = registerOTel();
registerInstrumentations({
  instrumentations: [new OpenAIInstrumentation()],
  tracerProvider: provider,
});

const client = new OpenAI();

const wrapped = wrapAgent("multi-step-agent", async ({ onComplete }, input: { userMessage: string }) => {
  const userMessage = input.userMessage;

  const intentResp = await client.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: "Extract the user's intent in one sentence." },
      { role: "user", content: userMessage },
    ],
  });
  const intent = intentResp.choices[0].message.content ?? "";

  const finalResp = await client.chat.completions.create({
    model: "gpt-4o",
    messages: [
      { role: "system", content: `User intent: ${intent}. Respond helpfully.` },
      { role: "user", content: userMessage },
    ],
  });
  const result = finalResp.choices[0].message.content ?? "";

  onComplete(result);
  return result;
});

const { result, runId } = await wrapped({ userMessage: "Help me plan a trip to Tokyo" });
The trace in Lemma shows:
  • One ai.agent.run root span with the user message as input and the final answer as output.
  • Two child spans — one per create call — each with its own prompt, response, model, and token counts.
Without provider instrumentation, wrap each LLM call in a manual step span instead. See TypeScript step usage or Python step usage.