Skip to main content

Automatically capture AI interactions

Optional setup for saving complete production interactions, timing, token usage, retrieval, and tool details.
You do not need this guide to write evals or run a local gate. Start with preferred and undesired response examples when you want the shortest path. When you need automatic production capture, EvalGate saves one complete user interaction as a trace. A model call, retrieval, or tool action inside that interaction is a span. Those records become searchable and can include timing, token, and cost details. This guide adds that optional capture to an application. OpenTelemetry collectors are an interoperability option, not a prerequisite.

Install the SDK

Set environment variables

Create a .env file in your project root and add your credentials:
.env
Get your API key from the Developer Dashboard.

Initialize the client and tracer

Create traces

A trace represents one logical operation — a user query, a support ticket, or a content generation request. Create a trace with a descriptive name and attach metadata that will help you filter it later:

Add spans

Spans represent individual steps within a trace — an LLM call, a vector search, or a function execution. Attach each span to its parent trace:

Nested spans for multi-step workflows

For pipelines with multiple sequential steps — like a RAG workflow with embedding, retrieval, and generation — use traceWorkflowStep to create properly nested spans automatically:

Adding custom metadata

Attach business context to traces to make them filterable and useful for debugging:

What a trace can hold

Traces and spans store what you or an SDK integration (such as traceOpenAI) send:

Viewing traces

Once your application is instrumented, open the Traces page in your dashboard to:
  • Search and filter traces by metadata, tags, or time range
  • View detailed timelines showing nested spans
  • Analyze token usage and costs across requests
  • Debug failures with full stack traces
  • Identify latency bottlenecks across pipeline steps

Best practices

Use descriptive names

Name traces after the user action, not the implementation. customer-support-query is more useful than llm-call.

Attach relevant metadata

Include userId, sessionId, environment, and feature flags so you can slice and debug traces effectively.

Sample for high volume

For high-throughput applications, sample in your application code (for example, 10–20% of requests) rather than tracing every call.

Never log PII

Anonymize or redact sensitive user data before it appears in trace inputs, outputs, or metadata fields.

Troubleshooting

Traces not appearing in the dashboard? Verify your EVALGATE_API_KEY is correct and that AIEvalClient is initialized before any traces are created. Noticing added latency? Each trace or span write is a network request. Keep explicit client.traces.create / createSpan calls off the user-facing critical path where you can; the traceOpenAI wrapper already records its trace without blocking the model response. Spans missing data? Ensure every async function inside a traceWorkflowStep callback is properly await-ed. Unawaited promises can resolve after the span closes.