Skip to main content
A generation is a single LLM call inside a trace. Record it with trace.recordGeneration() / trace.record_generation() after the model call completes.

Record a generation

await lemma.trace({ name: "support-agent", input: userMessage }, async (trace) => {
  const response = await callModel(messages);

  trace.recordGeneration({
    name: "draft-reply",
    input: messages,
    output: response.text,
    model: "gpt-4o",
    llmInputMessages: [{ role: "user", content: userMessage }],
    llmInvocationParameters: { temperature: 0.2 },
  });

  return response.text;
});

Measure a live generation

Use startGeneration() / start_generation() when you want a handle before the model call finishes. End it only after the provider returns or fails.
const generation = trace.startGeneration({
  name: "draft-reply",
  input: messages,
  model: "gpt-4o",
});

try {
  const response = await callModel(messages);
  generation.end({
    output: response.text,
    durationMs: response.durationMs,
  });
  return response.text;
} catch (error) {
  generation.end({ error });
  throw error;
}

What the SDK records

FieldAttribute keys
Generation kindLemma generation marker
Prompt / inputinput.value
Completion / outputoutput.value
Modelgen_ai.request.model, ai.model.id
Contract propsNative props such as llmInputMessages, llmInvocationParameters, and llmPromptTemplate; raw attributes for escape-hatch keys
See Native contract props for the full list of supported SDK props and the attributes they emit.

Errors

If a model call fails, record a failed generation before rethrowing:
try {
  const response = await callModel(messages);
  trace.recordGeneration({ name: "draft-reply", input: messages, output: response.text });
  return response.text;
} catch (error) {
  trace.recordGeneration({
    name: "draft-reply",
    input: messages,
    status: "ERROR",
    error,
  });
  throw error;
}

Next steps

Tool calls

Record tool arguments and results.

Spans

Trace retrieval, ranking, and app logic.