Skip to main content
A tool call is a single tool invocation inside a trace. Record it with trace.recordTool() / trace.record_tool() after the tool returns.

Record a tool call

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

  trace.recordTool({
    name: "search_docs",
    input: { query: userMessage },
    output: docs,
    durationMs: 25,
    toolParameters: { query: "string" },
  });

  return answerFromDocs(docs);
});
If the tool belongs under a measured parent span, record it as a child of that span:
const span = trace.startSpan("retrieve-context");
const docs = await searchDocs(userMessage);

span.recordTool({
  name: "search_docs",
  input: { query: userMessage },
  output: docs,
  durationMs: 25,
});
If the helper only has IDs, attach the tool from the client. This detached helper API is TypeScript-specific:
lemma.recordTool({
  traceId,
  parentSpanId: spanId,
  name: "search_docs",
  input: { query },
  output: docs,
  durationMs: 25,
});
traceId is required for detached tools. When the tool belongs under a span, parentSpanId is required; missing IDs warn and no-op. Pass durationMs when you already measured the tool call. If you omit it, Lemma splits the parent span’s remaining unclaimed duration equally across siblings that also omitted duration.

Measure a live tool call

Use startTool() / start_tool() when you want a handle before the tool finishes. End it only after the tool returns or fails.
const tool = trace.startTool({
  name: "search_docs",
  input: { query },
});

try {
  const docs = await searchDocs(query);
  tool.end({ output: docs, durationMs: 25 });
  return docs;
} catch (error) {
  tool.end({ error });
  throw error;
}

What the SDK records

FieldAttribute keys
Tool kindLemma tool marker
Tool nameSpan name and tool.name
Argumentsinput.value, ai.toolCall.args
Resultoutput.value, ai.toolCall.result
Contract propsNative props such as toolDescription and toolParameters; raw attributes for escape-hatch keys
See Native contract props for the full list of supported SDK props and the attributes they emit.

Record failures

try {
  const customer = await lookupCustomer(customerId);
  trace.recordTool({ name: "lookup_customer", input: { customerId }, output: customer });
  return customer;
} catch (error) {
  trace.recordTool({
    name: "lookup_customer",
    input: { customerId },
    status: "ERROR",
    error,
  });
  throw error;
}
Capture arguments and results only when safe. Redact secrets, credentials, and sensitive user data before passing them to traced tools.

Next steps

Spans

Trace retrieval, ranking, and app logic.

Threads & context

Group conversations and attach users.