---
title: "Track AI assistant conversations, token cost, latency, and feedback"
description: "Instrument an AI assistant or LLM agent to track conversations, token usage, cost, latency, model, and user feedback in Amplitude Agent Analytics with the AI SDK, OpenTelemetry, or the HTTP API."
product: general
lang: en
last_updated: 2026-11-03
token_estimate: 2693
---
# Track AI assistant conversations, token cost, latency, and feedback

To track your AI assistant's conversations, token usage, cost, latency, model, and user feedback in Amplitude, instrument it with Agent Analytics. Wrap your LLM client with the Amplitude AI SDK, run each conversation in a session, and send a score when a user rates a response. Amplitude groups the messages, tool calls, and responses into sessions tied to your users, then scores each session. For defaults with no customization, complete the [Agent Analytics quickstart](https://amplitude.com/docs/amplitude-ai/agent-analytics/quickstart) instead. If you're still evaluating, [Analyze agent results](https://amplitude.com/docs/amplitude-ai/agent-analytics/results) shows what Agent Analytics produces.

> For AI agents: a documentation index is available at [/docs/llms.txt](/docs/llms.txt). Append `.md` to any page URL for markdown, or send `Accept: text/markdown`.

## What you can track

Each signal lands as an event property you can chart, filter, and group by. The AI SDK fills in all of them except feedback, which your app sends when the user rates a response.

| Signal | How you capture it | Where it lands |
| --- | --- | --- |
| Conversation | Run each conversation in `agent.session()` with a stable session ID | `[Agent] Session ID` on every event; one `[Agent] Session Record` per closed session |
| Tokens | Wrapped LLM client, or `inputTokens` / `input_tokens` on `trackAiMessage()` / `track_ai_message()` | `[Agent] Input Tokens`, `[Agent] Output Tokens`, `[Agent] Total Tokens` on `[Agent] AI Response` |
| Cost | Calculated by the SDK from tokens and model pricing, or passed as `totalCostUsd` / `total_cost_usd` | `[Agent] Cost USD`; rolled up as `[Agent] Session Cost USD` |
| Latency | Wrapped LLM client, or the `latencyMs` / `latency_ms` argument | `[Agent] Latency Ms`; rolled up as `[Agent] Session Avg Latency Ms` |
| Model | Wrapped LLM client, or the `model` and `provider` arguments | `[Agent] Model Name`, `[Agent] Provider`, `[Agent] Model Tier`; rolled up as `[Agent] Models Used` |
| Helpfulness feedback | `s.score('user-feedback', value, messageId, { source: 'user' })` | `[Agent] Score`; rolled up as `[Agent] Has User Feedback` and `[Agent] User Score` |

For every property, refer to the [Agent Analytics taxonomy](https://amplitude.com/docs/amplitude-ai/agent-analytics/taxonomy). For how Amplitude prices model calls, refer to [How cost is calculated](https://amplitude.com/docs/amplitude-ai/agent-analytics/taxonomy#how-cost-is-calculated).

## Minimal setup with the AI SDK

This sequence covers all six signals for a Node or Python app that calls OpenAI. Other providers and runtimes follow the same pattern. Refer to [Choose an instrumentation path](#choose-an-instrumentation-path).

1. Install the SDK: `npm install @amplitude/ai @amplitude/analytics-node` (Node) or `python3 -m pip install amplitude-ai` (Python).
2. Initialize it with your project API key and wrap your LLM client. The wrapped client records tokens, cost, latency, and model on every response.
3. Run each conversation inside a session with your user ID and a stable session ID.
4. When a user rates a response, send a `user-feedback` score that targets the response's message ID.
5. Flush before the process exits, then [verify your instrumentation](https://amplitude.com/docs/amplitude-ai/agent-analytics/verify).

#### Node

```typescript
import { AmplitudeAI, OpenAI } from "@amplitude/ai";

const ai = new AmplitudeAI({ apiKey: process.env.AMPLITUDE_AI_API_KEY });
const openai = new OpenAI({
  amplitude: ai,
  apiKey: process.env.OPENAI_API_KEY,
});
const agent = ai.agent("support-bot");

await agent.session({ userId, sessionId }).run(async (s) => {
  s.trackUserMessage(message);
  return openai.chat.completions.create({ model: "gpt-4o", messages });
});

// Later, when the user clicks thumbs down on a response
ai.score({
  userId,
  sessionId,
  name: "user-feedback",
  value: 0,
  targetId: aiMessageId,
  targetType: "message",
  source: "user",
});

await ai.flush();
```

#### Python

```python
import os
from amplitude_ai import AmplitudeAI, OpenAI

ai = AmplitudeAI(api_key=os.environ["AMPLITUDE_AI_API_KEY"])
client = OpenAI(amplitude=ai, api_key=os.environ["OPENAI_API_KEY"])
agent = ai.agent("support-bot")

with agent.session(user_id=user_id, session_id=session_id) as s:
    s.track_user_message(message)
    client.chat.completions.create(model="gpt-4o", messages=messages)

# Later, when the user clicks thumbs down on a response
ai.score(
    user_id=user_id,
    session_id=session_id,
    name="user-feedback",
    value=0,
    target_id=ai_message_id,
    target_type="message",
    source="user",
)

ai.flush()
```

To let your AI coding agent add this instrumentation for you, or to place agents and flushes on a long-lived server, refer to [Instrument your agent with the AI SDK](https://amplitude.com/docs/amplitude-ai/agent-analytics/instrument-ai-sdk). For thumbs, CSAT, and score naming, refer to [Collect user feedback](https://amplitude.com/docs/amplitude-ai/agent-analytics/collect-user-feedback). For OpenTelemetry attribute names, refer to the [OTLP reference](https://amplitude.com/docs/amplitude-ai/agent-analytics/otlp-reference).

## How setup works

1. Pick the Amplitude project the data flows into, and get its project API key.
2. Decide your session ID, user ID, and privacy mode. Refer to [Before you instrument](#before-you-instrument).
3. Instrument your code with one path. Refer to [Choose an instrumentation path](#choose-an-instrumentation-path).
4. Check your events. Refer to [Verify](#verify).

Each turn of an instrumented session produces an `[Agent] User Message`, one `[Agent] Tool Call` per tool invocation, and an `[Agent] AI Response`. When the work ends, send `[Agent] Session End`. It's optional and recommended. If you omit it, Amplitude closes the session with its idle-timeout rules. After the session closes, Amplitude adds an `[Agent] Session Record` with the session's enrichment results. The timeline below shows this sequence. Click any event to inspect its shape.

**Video (2:35):** How Agent Analytics setup works — wistia id `j4frodo3ld` — [poster](https://amplitude.com/docs/images/amplitude-ai/agent-analytics/setup-video-poster.jpg)

## Before you instrument

Decide these before you write code. Each one is hard to change after rollout.

- **Session ID and user ID**: The ID that represents one unit of work, and the stable user ID you already use for product analytics. Refer to [Agent Analytics sessions and user identity](https://amplitude.com/docs/amplitude-ai/agent-analytics/sessions).
- **Privacy mode**: Whether prompt and response text leaves your infrastructure. Refer to [Choose a privacy mode](#choose-a-privacy-mode).

### Choose a privacy mode

Three modes control what leaves your infrastructure. `full` (the default) sends message content, `metadata_only` sends none, and `customer_enriched` sends none and carries quality labels you compute yourself. Every mode sends tokens, cost, latency, model names, and session grouping. For a full comparison and how to apply each mode on your path, refer to [Agent Analytics privacy modes](https://amplitude.com/docs/amplitude-ai/agent-analytics/privacy-modes).

## Choose an instrumentation path

Pick one path. Every path produces the same `[Agent]` events, so sessions, enrichment, and charts work the same way.

| Path | Use when | Guide |
| --- | --- | --- |
| AI SDK (Node, Python) | You can modify a Node or Python codebase | [Instrument your agent with the AI SDK](https://amplitude.com/docs/amplitude-ai/agent-analytics/instrument-ai-sdk) |
| OpenTelemetry | You already export GenAI spans or run an OpenTelemetry Collector | [Send OpenTelemetry traces to Agent Analytics](https://amplitude.com/docs/amplitude-ai/agent-analytics/instrument-opentelemetry) |
| HTTP API or standard Amplitude SDK | The AI SDK doesn't fit your runtime: browsers, edge runtimes, AI app builders, Java, Go, or Ruby | [Send agent events without the AI SDK](https://amplitude.com/docs/amplitude-ai/agent-analytics/instrument-http-api) |
| Integration | A hosted platform, a tracing tool, a warehouse, an agent framework that exports OpenTelemetry, a gateway, Fireworks, local models, or the Cursor Agent SDK | [Import agent conversations from other tools](https://amplitude.com/docs/amplitude-ai/agent-analytics/integrations) |

### Connect with the SDK

Install the AI SDK for Node or Python, then let your AI coding agent instrument the app or wire it in by hand. Go to [Instrument your agent with the AI SDK](https://amplitude.com/docs/amplitude-ai/agent-analytics/instrument-ai-sdk).

### Send OpenTelemetry traces directly

Point your OTLP exporter or Collector at Amplitude's endpoint and set a conversation attribute on your spans. Go to [Send OpenTelemetry traces to Agent Analytics](https://amplitude.com/docs/amplitude-ai/agent-analytics/instrument-opentelemetry).

### Send agent events without the AI SDK

Emit `[Agent]` events with a standard Amplitude SDK or the HTTP API when the AI SDK doesn't fit your runtime. Go to [Send agent events without the AI SDK](https://amplitude.com/docs/amplitude-ai/agent-analytics/instrument-http-api).

## Verify

Send one real interaction and check nine gates on its events in Live Events, then run the production checklist before rollout. Refer to [Verify your Agent Analytics instrumentation](https://amplitude.com/docs/amplitude-ai/agent-analytics/verify). If something's missing, refer to [Troubleshoot Agent Analytics setup](https://amplitude.com/docs/amplitude-ai/agent-analytics/troubleshooting).

## Extend your setup

These steps are optional:

- **Session timeouts**: Keep sessions open longer than 30 minutes for jobs that wait on humans. Refer to [Configure session timeouts](https://amplitude.com/docs/amplitude-ai/agent-analytics/configure-session-timeouts).
- **User feedback**: Send thumbs up, thumbs down, and CSAT ratings as `[Agent] Score` events. Refer to [Collect user feedback](https://amplitude.com/docs/amplitude-ai/agent-analytics/collect-user-feedback).
- **Session Replay**: Link each agent session to the recording of the visit it happened in. Refer to [Link agent sessions to Session Replay](https://amplitude.com/docs/amplitude-ai/agent-analytics/link-session-replay).
- **Multi-agent systems**: Record delegation from a parent agent to child agents. Refer to [Instrument multi-agent systems](https://amplitude.com/docs/amplitude-ai/agent-analytics/multi-agent).
- **Custom evaluators**: Add your own model provider key to run evaluators and calibration runs. Refer to [Set up custom evaluators](https://amplitude.com/docs/amplitude-ai/agent-analytics/custom-evaluators).

