@amplitude/ai 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/.claude/commands/instrument-with-amplitude-ai.md +12 -0
  2. package/.cursor/skills/instrument-with-amplitude-ai/SKILL.md +4 -42
  3. package/AGENTS.md +86 -28
  4. package/README.md +190 -111
  5. package/amplitude-ai.md +463 -0
  6. package/bin/amplitude-ai-doctor.mjs +0 -0
  7. package/bin/amplitude-ai-instrument.mjs +0 -0
  8. package/bin/amplitude-ai-mcp.mjs +0 -0
  9. package/bin/amplitude-ai-register-catalog.mjs +0 -0
  10. package/bin/amplitude-ai-status.mjs +0 -0
  11. package/bin/amplitude-ai.mjs +14 -5
  12. package/data/agent_event_catalog.json +52 -2
  13. package/dist/bound-agent.d.ts +18 -14
  14. package/dist/bound-agent.d.ts.map +1 -1
  15. package/dist/bound-agent.js +4 -1
  16. package/dist/bound-agent.js.map +1 -1
  17. package/dist/cli/status.d.ts.map +1 -1
  18. package/dist/cli/status.js +31 -19
  19. package/dist/cli/status.js.map +1 -1
  20. package/dist/client.d.ts +14 -14
  21. package/dist/client.d.ts.map +1 -1
  22. package/dist/client.js +38 -0
  23. package/dist/client.js.map +1 -1
  24. package/dist/context.d.ts +5 -0
  25. package/dist/context.d.ts.map +1 -1
  26. package/dist/context.js +4 -0
  27. package/dist/context.js.map +1 -1
  28. package/dist/core/constants.d.ts +3 -1
  29. package/dist/core/constants.d.ts.map +1 -1
  30. package/dist/core/constants.js +3 -1
  31. package/dist/core/constants.js.map +1 -1
  32. package/dist/core/tracking.d.ts +12 -2
  33. package/dist/core/tracking.d.ts.map +1 -1
  34. package/dist/core/tracking.js +13 -2
  35. package/dist/core/tracking.js.map +1 -1
  36. package/dist/decorators.d.ts +1 -1
  37. package/dist/decorators.d.ts.map +1 -1
  38. package/dist/decorators.js +4 -3
  39. package/dist/decorators.js.map +1 -1
  40. package/dist/index.d.ts +7 -4
  41. package/dist/index.js +5 -2
  42. package/dist/mcp/contract.d.ts +4 -0
  43. package/dist/mcp/contract.d.ts.map +1 -1
  44. package/dist/mcp/contract.js +6 -2
  45. package/dist/mcp/contract.js.map +1 -1
  46. package/dist/mcp/generate-verify-test.d.ts +7 -0
  47. package/dist/mcp/generate-verify-test.d.ts.map +1 -0
  48. package/dist/mcp/generate-verify-test.js +25 -0
  49. package/dist/mcp/generate-verify-test.js.map +1 -0
  50. package/dist/mcp/index.d.ts +2 -1
  51. package/dist/mcp/index.js +2 -1
  52. package/dist/mcp/instrument-file.d.ts +14 -0
  53. package/dist/mcp/instrument-file.d.ts.map +1 -0
  54. package/dist/mcp/instrument-file.js +139 -0
  55. package/dist/mcp/instrument-file.js.map +1 -0
  56. package/dist/mcp/scan-project.d.ts +52 -0
  57. package/dist/mcp/scan-project.d.ts.map +1 -0
  58. package/dist/mcp/scan-project.js +309 -0
  59. package/dist/mcp/scan-project.js.map +1 -0
  60. package/dist/mcp/server.d.ts.map +1 -1
  61. package/dist/mcp/server.js +79 -4
  62. package/dist/mcp/server.js.map +1 -1
  63. package/dist/mcp/validate-file.d.ts +4 -0
  64. package/dist/mcp/validate-file.d.ts.map +1 -1
  65. package/dist/mcp/validate-file.js +559 -11
  66. package/dist/mcp/validate-file.js.map +1 -1
  67. package/dist/middleware.js +2 -1
  68. package/dist/middleware.js.map +1 -1
  69. package/dist/node_modules/.pnpm/acorn-typescript@1.4.13_acorn@8.16.0/node_modules/acorn-typescript/lib/index.js +2389 -0
  70. package/dist/node_modules/.pnpm/acorn-typescript@1.4.13_acorn@8.16.0/node_modules/acorn-typescript/lib/index.js.map +1 -0
  71. package/dist/node_modules/.pnpm/acorn@8.16.0/node_modules/acorn/dist/acorn.js +5128 -0
  72. package/dist/node_modules/.pnpm/acorn@8.16.0/node_modules/acorn/dist/acorn.js.map +1 -0
  73. package/dist/node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/types.js +1 -1
  74. package/dist/patching.d.ts.map +1 -1
  75. package/dist/providers/anthropic.d.ts.map +1 -1
  76. package/dist/providers/anthropic.js +1 -0
  77. package/dist/providers/anthropic.js.map +1 -1
  78. package/dist/providers/base.d.ts +2 -1
  79. package/dist/providers/base.d.ts.map +1 -1
  80. package/dist/providers/base.js +4 -0
  81. package/dist/providers/base.js.map +1 -1
  82. package/dist/providers/openai.d.ts.map +1 -1
  83. package/dist/providers/openai.js +2 -0
  84. package/dist/providers/openai.js.map +1 -1
  85. package/dist/serverless.d.ts +19 -0
  86. package/dist/serverless.d.ts.map +1 -0
  87. package/dist/serverless.js +35 -0
  88. package/dist/serverless.js.map +1 -0
  89. package/dist/session.d.ts +24 -8
  90. package/dist/session.d.ts.map +1 -1
  91. package/dist/session.js +20 -1
  92. package/dist/session.js.map +1 -1
  93. package/dist/types.d.ts +1 -0
  94. package/dist/types.d.ts.map +1 -1
  95. package/dist/types.js.map +1 -1
  96. package/llms-full.txt +353 -69
  97. package/llms.txt +6 -2
  98. package/mcp.schema.json +7 -3
  99. package/package.json +10 -5
  100. package/bin/amplitude-ai-init.mjs +0 -27
  101. package/dist/cli/init.d.ts +0 -14
  102. package/dist/cli/init.d.ts.map +0 -1
  103. package/dist/cli/init.js +0 -40
  104. package/dist/cli/init.js.map +0 -1
package/README.md CHANGED
@@ -4,48 +4,81 @@
4
4
  [![CI](https://github.com/amplitude/Amplitude-AI-Node/actions/workflows/test.yml/badge.svg)](https://github.com/amplitude/Amplitude-AI-Node/actions/workflows/test.yml)
5
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
6
 
7
- Agent analytics for [Amplitude](https://amplitude.com). Track every LLM call as events in your Amplitude project, then build funnels, cohorts, and retention charts across AI and product behavior.
7
+ Agent analytics for [Amplitude](https://amplitude.com). Track every LLM call, user message, tool call, and quality signal as events in your Amplitude project then build funnels, cohorts, and retention charts across AI and product behavior.
8
8
 
9
9
  ```bash
10
10
  npm install @amplitude/ai @amplitude/analytics-node
11
11
  ```
12
12
 
13
13
  ```typescript
14
- import { AmplitudeAI, patch } from '@amplitude/ai';
15
- // Your existing code — unchanged
16
- import OpenAI from 'openai';
14
+ import { AmplitudeAI, OpenAI } from '@amplitude/ai';
17
15
 
18
16
  const ai = new AmplitudeAI({ apiKey: process.env.AMPLITUDE_AI_API_KEY! });
19
- patch({ amplitudeAI: ai });
17
+ const openai = new OpenAI({ amplitude: ai, apiKey: process.env.OPENAI_API_KEY });
18
+ const agent = ai.agent('my-agent');
20
19
 
21
- const response = await new OpenAI().chat.completions.create({
22
- model: 'gpt-4o',
23
- messages: [{ role: 'user', content: 'What is retention?' }],
20
+ app.post('/chat', async (req, res) => {
21
+ const session = agent.session({ userId: req.userId, sessionId: req.sessionId });
22
+
23
+ const result = await session.run(async (s) => {
24
+ s.trackUserMessage(req.body.message);
25
+ const response = await openai.chat.completions.create({
26
+ model: 'gpt-4o',
27
+ messages: req.body.messages,
28
+ });
29
+ return response.choices[0].message.content;
30
+ });
31
+
32
+ await ai.flush();
33
+ res.json({ response: result });
24
34
  });
25
- // [Agent] User Message + [Agent] AI Response now in your Amplitude project
35
+ // Events: [Agent] User Message, [Agent] AI Response (with model, tokens, cost, latency),
36
+ // [Agent] Session Start, [Agent] Session End — all tied to userId and sessionId
26
37
  ```
27
38
 
28
- One call auto-detects and patches every installed provider (OpenAI, Anthropic, Azure OpenAI, Gemini, Mistral, Bedrock). Want more control? See [Choose Your Integration Tier](#choose-your-integration-tier) below.
39
+ ## How to Get Started
40
+
41
+ ### Instrument with a coding agent (recommended)
42
+
43
+ ```bash
44
+ npm install @amplitude/ai
45
+ npx amplitude-ai
46
+ ```
47
+
48
+ The CLI prints a prompt to paste into any AI coding agent (Cursor, Claude Code, Windsurf, Copilot, Codex, etc.):
49
+
50
+ > Instrument this app with @amplitude/ai. Follow node_modules/@amplitude/ai/amplitude-ai.md
51
+
52
+ The agent reads the guide, scans your project, discovers your agents and LLM call sites, and instruments everything — provider wrappers, session lifecycle, multi-agent delegation, tool tracking, scoring, and a verification test. You review and approve each step.
53
+
54
+ ### Other paths
55
+
56
+ | Your situation | Recommended path | What happens |
57
+ |---|---|---|
58
+ | **Manual setup** | Follow the [code example above](#amplitude-ai) | Agents + sessions + provider wrappers — the full event model |
59
+ | **Just want to verify the SDK works** | `patch()` ([details below](#patching-diagnostic--legacy)) | Aggregate cost/latency monitoring only — no user analytics, no funnels |
60
+
61
+ > **Start with full instrumentation.** The coding agent workflow defaults to agents + sessions + provider wrappers. This gives you every event type, per-user analytics, and server-side enrichment. `patch()` exists for quick verification or legacy codebases where you can't modify call sites, but it only captures `[Agent] AI Response` without user identity — no funnels, no cohorts, no retention.
29
62
 
30
63
  | Property | Value |
31
64
  | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
32
65
  | Name | @amplitude/ai |
33
- | Version | 0.1.0 |
66
+ | Version | 0.2.1 |
34
67
  | Runtime | Node.js |
35
68
  | Peer dependency | @amplitude/analytics-node >= 1.3.0 |
36
69
  | Optional peers | openai, @anthropic-ai/sdk, @google/generative-ai, @mistralai/mistralai, @aws-sdk/client-bedrock-runtime, @pydantic/genai-prices (cost), tiktoken or js-tiktoken (token counting) |
37
70
 
38
71
  ## Table of Contents
39
72
 
73
+ - [How to Get Started](#how-to-get-started)
74
+ - [Instrument with a Coding Agent (recommended)](#instrument-with-a-coding-agent-recommended)
40
75
  - [Installation](#installation)
41
76
  - [Quick Start](#quick-start)
42
77
  - [Current Limitations](#current-limitations)
43
78
  - [Is this for me?](#is-this-for-me)
44
79
  - [Why this SDK?](#why-this-sdk)
45
80
  - [What you can build](#what-you-can-build)
46
- - [Choose Your Integration Tier](#choose-your-integration-tier)
47
- - [Support matrix](#support-matrix)
48
- - [Parity and runtime limitations](#parity-and-runtime-limitations)
81
+ - [What You Get at Each Level](#what-you-get-at-each-level)
49
82
  - [Core Concepts](#core-concepts)
50
83
  - [User Identity](#user-identity)
51
84
  - [Session](#session)
@@ -63,7 +96,7 @@ One call auto-detects and patches every installed provider (OpenAI, Anthropic, A
63
96
  - [Scoring Patterns](#scoring-patterns)
64
97
  - [Enrichments](#enrichments)
65
98
  - [Debug and Dry-Run Modes](#debug-and-dry-run-modes)
66
- - [Patching (Zero-Code Instrumentation)](#patching-zero-code-instrumentation)
99
+ - [Patching (Diagnostic / Legacy)](#patching-diagnostic--legacy)
67
100
  - [Auto-Instrumentation CLI](#auto-instrumentation-cli)
68
101
  - [Integrations](#integrations)
69
102
  - [Data Flow](#data-flow)
@@ -100,68 +133,25 @@ Install provider SDKs based on what you use (for example: `openai`, `@anthropic-
100
133
 
101
134
  ## Quick Start
102
135
 
103
- ### Quickstart (5 minutes)
136
+ ### 5-minute quick start
104
137
 
105
138
  1. **Install:** `npm install @amplitude/ai @amplitude/analytics-node`
106
139
  2. **Get your API key:** In Amplitude, go to **Settings > Projects** and copy the API key.
107
- 3. **Add two lines** to the top of your app (see the hero example above).
108
- 4. **Run your app.** Any OpenAI, Anthropic, Gemini, or Mistral call is now instrumented automatically.
109
- 5. **Open Amplitude > Events.** You should see `[Agent] AI Response` within 30 seconds.
140
+ 3. **Auto-instrument:** Run `npx amplitude-ai` and paste the printed prompt into your AI coding agent — it scans your project, generates a bootstrap file, instruments your LLM call sites, and creates a verification test. Or follow the manual patterns below.
141
+ 4. **Set your API key** in the generated `.env` file and replace the placeholder `userId`/`sessionId`.
142
+ 5. **Run your app.** You should see `[Agent] User Message`, `[Agent] AI Response`, and `[Agent] Session End` within 30 seconds.
110
143
 
111
144
  To verify locally before checking Amplitude, add `debug: true`:
112
145
 
113
146
  ```typescript
114
- import { AIConfig, AmplitudeAI, patch } from '@amplitude/ai';
115
-
116
147
  const ai = new AmplitudeAI({
117
148
  apiKey: process.env.AMPLITUDE_AI_API_KEY!,
118
149
  config: new AIConfig({ debug: true }),
119
150
  });
120
- patch({ amplitudeAI: ai });
121
151
  // Prints: [amplitude-ai] [Agent] AI Response | model=gpt-4o | tokens=847 | cost=$0.0042 | latency=1,203ms
122
152
  ```
123
153
 
124
- ### Full-control quick start
125
-
126
- For maximum flexibility, use `BoundAgent` + `Session` with explicit tracking calls:
127
-
128
- ```typescript
129
- import { AmplitudeAI } from '@amplitude/ai';
130
-
131
- const ai = new AmplitudeAI({ apiKey: process.env.AMPLITUDE_AI_API_KEY! });
132
-
133
- const agent = ai.agent('my-agent', { userId: 'user-123' });
134
- const session = agent.session();
135
-
136
- await session.run(async (s) => {
137
- s.trackUserMessage('What is the capital of France?');
138
-
139
- const response = await callLLM('What is the capital of France?');
140
-
141
- s.trackAiMessage(
142
- response.content,
143
- response.model,
144
- 'openai',
145
- response.latencyMs,
146
- {
147
- inputTokens: response.inputTokens,
148
- outputTokens: response.outputTokens,
149
- },
150
- );
151
- });
152
- ```
153
-
154
- ### What the quick setup gives you
155
-
156
- The zero-code / CLI setup gives you cost, latency, token counts, and error tracking immediately. But cohorts, retention analysis, funnels, and server-side enrichment all require `userId` and `sessionId`. Without them you have aggregate monitoring — not per-user analytics.
157
-
158
- | Setup | What you get |
159
- | -------------------- | ------------------------------------------------- |
160
- | CLI / zero-code only | Cost, latency, tokens, errors — aggregate only |
161
- | + `userId` | Per-user funnels, cohorts, retention |
162
- | + `sessionId` | Session grouping, enrichment, behavioral patterns |
163
-
164
- Adding `userId` is one option per call. Adding session context is `session.run()`. See [Session](#session) and [Choose Your Integration Tier](#choose-your-integration-tier).
154
+ > **Tip:** Call `enableLivePriceUpdates()` at startup so cost tracking stays accurate when new models are released. See [Cache-Aware Cost Tracking](#cache-aware-cost-tracking).
165
155
 
166
156
  ### Current Limitations
167
157
 
@@ -191,7 +181,9 @@ The structural difference is the event model. Trace-centric tools typically prod
191
181
 
192
182
  **Every AI event carries your product `user_id`.** No separate identity system, no data joining required. Build a funnel from "user opens chat" to "AI responds" to "user upgrades" directly in Amplitude.
193
183
 
194
- **Server-side enrichment does the evals for you.** When content is available (`contentMode: 'full'`), Amplitude's enrichment pipeline runs automatically on every session after it closes. You get topic classifications, quality rubrics, behavioral flags, and session outcomes without writing or maintaining any eval code.
184
+ **Server-side enrichment does the evals for you.** When content is available (`contentMode: 'full'`), Amplitude's enrichment pipeline runs automatically on every session after it closes. You get topic classifications, quality rubrics, behavioral flags, and session outcomes without writing or maintaining any eval code. Define your own topics and scoring rubrics; the pipeline applies them to every session automatically. Results appear as `[Agent] Score` events with rubric scores, `[Agent] Topic Classification` events with category labels, and `[Agent] Session Evaluation` summaries, all queryable in charts, cohorts, and funnels alongside your product events.
185
+
186
+ **Quality signals from every source in one event type.** User thumbs up/down (`source: 'user'`), automated rubric scores from the enrichment pipeline (`source: 'ai'`), and reviewer assessments (`source: 'reviewer'`) all produce `[Agent] Score` events differentiated by `[Agent] Evaluation Source`. One chart shows all three side by side. Filter by source or view them together. Filter by `[Agent] Agent ID` for per-agent quality attribution.
195
187
 
196
188
  **Three content-control tiers.** `full` sends content and Amplitude runs enrichments for you. `metadata_only` sends zero content (you still get cost, latency, tokens, session grouping). `customer_enriched` sends zero content but lets you provide your own structured labels via `trackSessionEnrichment()`.
197
189
 
@@ -206,13 +198,69 @@ Once AI events are in Amplitude alongside your product events:
206
198
  - **Retention.** Do users with successful AI sessions retain better than those with failures? Segment retention curves by `[Agent] Overall Outcome` or task completion score.
207
199
  - **Agent analytics.** Compare quality, cost, and failure rate across agents in one chart. Identify which agent in a multi-agent chain introduced a failure.
208
200
 
209
- ## Choose Your Integration Tier
201
+ ### How quality measurement works
202
+
203
+ The SDK captures quality signals at three layers, from most direct to most comprehensive:
204
+
205
+ **1. Explicit user feedback** — Instrument thumbs up/down, star ratings, or CSAT scores via [`trackScore()`](#scoring-patterns). Each call produces an `[Agent] Score` event with `source: 'user'`:
206
+
207
+ ```typescript
208
+ ai.trackScore({
209
+ userId: 'u1', name: 'user-feedback', value: 1,
210
+ targetId: aiMessageId, targetType: 'message', source: 'user',
211
+ });
212
+ ```
213
+
214
+ **2. Implicit behavioral signals** — The SDK auto-tracks behavioral proxies for quality on every turn, with zero additional instrumentation:
210
215
 
211
- | Tier | Code Changes | What You Get |
212
- | --------------------- | ----------------------------------------------------- | ------------------------------------------------- |
213
- | **Zero-code (patch)** | 2 lines | Auto-tracks supported provider calls via patching |
214
- | **Wrap** | `wrap(client, ai)` or `new OpenAI({ amplitude: ai })` | Auto-tracking + full control of options per call |
215
- | **Full control** | Call `trackUserMessage`/`trackAiMessage` directly | Maximum flexibility, works with any LLM provider |
216
+ | Signal | Property | Event | Interpretation |
217
+ |--------|----------|-------|----------------|
218
+ | Copy | `[Agent] Was Copied` | `[Agent] AI Response` | User copied the output positive |
219
+ | Regeneration | `[Agent] Is Regeneration` | `[Agent] User Message` | User asked for a redo negative |
220
+ | Edit | `[Agent] Is Edit` | `[Agent] User Message` | User refined their prompt friction |
221
+ | Abandonment | `[Agent] Abandonment Turn` | `[Agent] Session End` | User left after N turns — potential failure |
222
+
223
+ **3. Automated server-side evaluation** — When `contentMode: 'full'`, Amplitude's enrichment pipeline runs LLM-as-judge evaluators on every session after it closes. No eval code to write or maintain:
224
+
225
+ | Rubric | What it measures | Scale |
226
+ |--------|-----------------|-------|
227
+ | `task_completion` | Did the agent accomplish what the user asked? | 0–2 |
228
+ | `response_quality` | Was the response clear, accurate, and helpful? | 0–2 |
229
+ | `user_satisfaction` | Did the user seem satisfied based on conversation signals? | 0–2 |
230
+ | `agent_confusion` | Did the agent misunderstand or go off track? | 0–2 |
231
+
232
+ Plus boolean detectors: `negative_feedback` (frustration phrases), `task_failure` (agent failed to deliver), `data_quality_issues`, and `behavioral_patterns` (clarification loops, topic drift). All results are emitted as `[Agent] Score` events with `source: 'ai'`.
233
+
234
+ **All three layers use the same `[Agent] Score` event type**, differentiated by `[Agent] Evaluation Source` (`'user'`, `'ai'`, or `'reviewer'`). One chart shows user feedback alongside automated evals. No joins, no separate tables.
235
+
236
+ ## What You Set vs What You Get
237
+
238
+ | You set | Where it comes from | What you unlock |
239
+ |---|---|---|
240
+ | API key | Amplitude project settings | Events reach Amplitude |
241
+ | userId | Your auth layer (JWT, session cookie, API token) | Per-user analytics, cohorts, retention |
242
+ | agentId | Your choice (e.g. `'chat-handler'`) | Per-agent cost, latency, quality dashboards |
243
+ | sessionId | Your conversation/thread/ticket ID | Multi-turn analysis, session enrichment, quality scores |
244
+ | *description* | *Your choice (e.g. `'Handles support queries via GPT-4o'`)* | *Human-readable agent registry from event streams* |
245
+ | *contentMode + redactPii* | *Config (defaults work)* | *Server enrichment (automatic), PII scrubbing* |
246
+ | *model, tokens, cost* | *Auto-captured by provider wrappers* | *Cost analytics, latency monitoring* |
247
+ | parentAgentId | Auto via `child()`/`runAs()` | Multi-agent hierarchy |
248
+ | env, agentVersion, context | Your deploy pipeline | Segmentation, regression detection |
249
+
250
+ *Italicized rows require zero developer effort — they're automatic or have sensible defaults.*
251
+
252
+ **The minimum viable setup is 4 fields**: API key, userId, agentId, sessionId.
253
+ Everything else is either automatic or a progressive enhancement.
254
+
255
+ ## What You Get at Each Level
256
+
257
+ The coding agent workflow defaults to **full instrumentation** — the top row below. Lower levels exist as fallbacks, not as recommended starting points.
258
+
259
+ | Level | Events you get | What it unlocks in Amplitude |
260
+ |---|---|---|
261
+ | **Full** (agents + sessions + wrappers) | User Message, AI Response, Tool Call, Session Start/End, Score, Enrichments | Per-user funnels, cohorts, retention, session replay linking, quality scoring |
262
+ | **Wrappers only** (no sessions) | AI Response (with cost, tokens, latency) | Aggregate cost monitoring, model comparison |
263
+ | **`patch()` only** (no wrappers, no sessions) | AI Response (basic) | Aggregate call counts — useful for verification only |
216
264
 
217
265
  ### Support matrix
218
266
 
@@ -231,9 +279,9 @@ This section is the source of truth for behavior that is intentionally different
231
279
  - Auto-instrument bootstrap differs by runtime (`node --import` in Node vs `sitecustomize` in Python).
232
280
  - Request middleware differs by runtime (Express-compatible in Node vs ASGI middleware in Python).
233
281
 
234
- ### Zero-code (recommended for getting started)
282
+ ### Zero-code (for verification or legacy codebases)
235
283
 
236
- Add two lines at application startup and LLM calls through supported patched providers are tracked automatically:
284
+ `patch()` monkey-patches provider SDKs so existing LLM calls are tracked without code changes. This is useful for verifying the SDK works or for legacy codebases where you can't modify call sites. It only captures `[Agent] AI Response` without user identity — for the full event model, use agents + sessions (see [Quick Start](#quick-start)).
237
285
 
238
286
  ```typescript
239
287
  import { AmplitudeAI, patch } from '@amplitude/ai';
@@ -354,23 +402,30 @@ const ai = new AmplitudeAI({ amplitude: existingAmplitudeClient });
354
402
 
355
403
  ### BoundAgent
356
404
 
357
- Agent with pre-bound defaults (`agentId`, `userId`, `env`, etc.). Use `agent()` to create:
405
+ Agent with pre-bound defaults (`agentId`, `description`, `userId`, `env`, etc.). Use `agent()` to create:
358
406
 
359
407
  ```typescript
360
408
  const agent = ai.agent('support-bot', {
409
+ description: 'Handles customer support queries via OpenAI GPT-4o',
361
410
  userId: 'user-123',
362
411
  env: 'production',
363
412
  customerOrgId: 'org-456',
364
413
  });
365
414
  ```
366
415
 
367
- Child agents inherit context from their parent and automatically set `parentAgentId`:
416
+ Child agents inherit context (including `description`) from their parent and automatically set `parentAgentId`:
368
417
 
369
418
  ```typescript
370
- const orchestrator = ai.agent('orchestrator', { userId: 'user-123' });
419
+ const orchestrator = ai.agent('orchestrator', {
420
+ description: 'Routes queries to specialized child agents',
421
+ userId: 'user-123',
422
+ });
371
423
  const researcher = orchestrator.child('researcher');
372
- const writer = orchestrator.child('writer');
424
+ const writer = orchestrator.child('writer', {
425
+ description: 'Drafts responses using retrieved context',
426
+ });
373
427
  // researcher.parentAgentId === 'orchestrator'
428
+ // researcher inherits orchestrator's description; writer has its own
374
429
  ```
375
430
 
376
431
  ### TenantHandle
@@ -560,6 +615,7 @@ The `context` parameter on `ai.agent()` accepts an arbitrary `Record<string, unk
560
615
  ```typescript
561
616
  const agent = ai.agent('support-bot', {
562
617
  userId: 'u1',
618
+ description: 'Handles customer support queries via OpenAI GPT-4o',
563
619
  agentVersion: '4.2.0',
564
620
  context: {
565
621
  agent_type: 'executor',
@@ -1155,7 +1211,10 @@ const ai = new AmplitudeAI({
1155
1211
  }),
1156
1212
  });
1157
1213
 
1158
- const agent = ai.agent('support-bot', { agentVersion: '2.1.0' });
1214
+ const agent = ai.agent('support-bot', {
1215
+ description: 'Handles support conversations in metadata-only mode',
1216
+ agentVersion: '2.1.0',
1217
+ });
1159
1218
 
1160
1219
  // 2. Run the conversation — content is NOT sent (metadata only)
1161
1220
  const session = agent.session({ userId: 'user-42' });
@@ -1308,9 +1367,9 @@ Both modes can be enabled via environment variables when using auto-instrumentat
1308
1367
  AMPLITUDE_AI_DEBUG=true amplitude-ai-instrument node app.js
1309
1368
  ```
1310
1369
 
1311
- ## Patching (Zero-Code Instrumentation)
1370
+ ## Patching (Diagnostic / Legacy)
1312
1371
 
1313
- Monkey-patch provider SDKs to auto-track without changing call sites:
1372
+ Monkey-patch provider SDKs to auto-track without changing call sites. This is useful for quick verification that the SDK is connected, or for legacy codebases where modifying call sites is impractical. For the full event model (user messages, sessions, scoring, enrichments), use agents + sessions as shown in [Quick Start](#quick-start).
1314
1373
 
1315
1374
  ```typescript
1316
1375
  import {
@@ -1366,13 +1425,7 @@ Environment variables:
1366
1425
  | `AMPLITUDE_AI_CONTENT_MODE` | `full`, `metadata_only`, or `customer_enriched` |
1367
1426
  | `AMPLITUDE_AI_DEBUG` | `"true"` for debug output to stderr |
1368
1427
 
1369
- ### Init and Doctor CLI
1370
-
1371
- Scaffold a minimal setup:
1372
-
1373
- ```bash
1374
- amplitude-ai init
1375
- ```
1428
+ ### Doctor CLI
1376
1429
 
1377
1430
  Validate setup (env, provider deps, mock event capture, mock flush path):
1378
1431
 
@@ -1380,12 +1433,8 @@ Validate setup (env, provider deps, mock event capture, mock flush path):
1380
1433
  amplitude-ai doctor
1381
1434
  ```
1382
1435
 
1383
- When a check fails, `doctor` includes an actionable `fix` field with a copy-pasteable remediation command (e.g., `export AMPLITUDE_AI_API_KEY=...`, `pnpm add openai`).
1384
-
1385
1436
  Useful flags:
1386
1437
 
1387
- - `amplitude-ai init --dry-run`
1388
- - `amplitude-ai init --force`
1389
1438
  - `amplitude-ai doctor --no-mock-check`
1390
1439
 
1391
1440
  ### Status
@@ -1418,20 +1467,25 @@ amplitude-ai mcp
1418
1467
 
1419
1468
  MCP surface:
1420
1469
 
1421
- | Tool | Description |
1422
- | ------------------------- | ------------------------------------------------------------- |
1423
- | `get_event_schema` | Return the full event schema and property definitions |
1424
- | `get_integration_pattern` | Return canonical instrumentation code patterns |
1425
- | `validate_setup` | Check env vars and dependency presence |
1426
- | `suggest_instrumentation` | Context-aware next steps based on your framework and provider |
1427
- | `validate_file` | Analyze source code to detect uninstrumented LLM call sites |
1470
+ | Tool | Description |
1471
+ | ------------------------- | -------------------------------------------------------------------------- |
1472
+ | `scan_project` | Scan project structure, detect providers, frameworks, and multi-agent patterns |
1473
+ | `validate_file` | Analyze a source file to detect uninstrumented LLM call sites |
1474
+ | `instrument_file` | Apply instrumentation transforms to a source file |
1475
+ | `generate_verify_test` | Generate a dry-run verification test using MockAmplitudeAI |
1476
+ | `get_event_schema` | Return the full event schema and property definitions |
1477
+ | `get_integration_pattern` | Return canonical instrumentation code patterns |
1478
+ | `validate_setup` | Check env vars and dependency presence |
1479
+ | `suggest_instrumentation` | Context-aware next steps based on your framework and provider |
1480
+ | `search_docs` | Full-text search across SDK documentation (README, llms-full.txt) |
1428
1481
 
1429
- Resources: `amplitude-ai://event-schema`, `amplitude-ai://integration-patterns`
1482
+ Resources: `amplitude-ai://event-schema`, `amplitude-ai://integration-patterns`, `amplitude-ai://instrument-guide`
1430
1483
 
1431
1484
  Prompt: `instrument_app` — guided walkthrough for instrumenting an application
1432
1485
 
1433
- ### Examples and Cursor Skill
1486
+ ### Examples and AI Coding Agent Guide
1434
1487
 
1488
+ - **`amplitude-ai.md`** — self-contained instrumentation guide for any AI coding agent (Cursor, Claude Code, Windsurf, Copilot, Codex, etc.). Run `npx amplitude-ai` to see the prompt that points your agent to this file.
1435
1489
  - Mock-based examples demonstrating the event model (also used as CI smoke tests):
1436
1490
  - `examples/zero-code.ts`
1437
1491
  - `examples/wrap-openai.ts`
@@ -1440,8 +1494,6 @@ Prompt: `instrument_app` — guided walkthrough for instrumenting an application
1440
1494
  - Real provider examples (require API keys):
1441
1495
  - `examples/real-openai.ts` — end-to-end OpenAI integration with session tracking and flush
1442
1496
  - `examples/real-anthropic.ts` — end-to-end Anthropic integration with session tracking and flush
1443
- - Cursor skill:
1444
- - `.cursor/skills/instrument-with-amplitude-ai/SKILL.md`
1445
1497
 
1446
1498
  ## Integrations
1447
1499
 
@@ -1763,7 +1815,18 @@ await session.run(async (s) => {
1763
1815
 
1764
1816
  ## Serverless Environments
1765
1817
 
1766
- **Critical**: Always `await ai.flush()` before your handler returns. Without this, events buffered in memory are lost when the runtime freezes or terminates:
1818
+ The SDK auto-detects serverless environments (Vercel, AWS Lambda, Netlify, Google Cloud Functions, Azure Functions, Cloudflare Pages). When detected, `session.run()` automatically flushes all pending events before the promise resolves — no explicit `ai.flush()` needed. You can also control this explicitly via the `autoFlush` option on `session()`:
1819
+
1820
+ ```typescript
1821
+ // Auto-detected: flushes automatically in serverless, skips in long-running servers
1822
+ agent.session({ userId, sessionId });
1823
+
1824
+ // Explicit control:
1825
+ agent.session({ userId, sessionId, autoFlush: true }); // always flush
1826
+ agent.session({ userId, sessionId, autoFlush: false }); // never flush
1827
+ ```
1828
+
1829
+ If you track events **outside** of `session.run()`, you still need `await ai.flush()` before your handler returns:
1767
1830
 
1768
1831
  ```typescript
1769
1832
  export async function handler(event: APIGatewayEvent) {
@@ -2030,6 +2093,7 @@ Event-specific properties for `[Agent] User Message` (in addition to common prop
2030
2093
  | `[Agent] Total Attachment Size Bytes` | number | No | Total size of all attachments in bytes. |
2031
2094
  | `[Agent] Attachments` | string | No | Serialized JSON array of attachment metadata (type, name, size_bytes, mime_type). Only metadata, never file content. |
2032
2095
  | `[Agent] Message Labels` | string | No | Serialized JSON array of MessageLabel objects (key-value pairs with optional confidence). Used for routing tags, classifier output, business context. |
2096
+ | `[Agent] Message Source` | string | No | Origin of the message: `'user'` for direct user input, `'agent'` for messages delegated from a parent agent. Auto-set based on `parentAgentId`. |
2033
2097
 
2034
2098
  ### AI Response Properties
2035
2099
 
@@ -2478,17 +2542,32 @@ See `src/core/tracking.ts` and `src/core/constants.ts` for the full list.
2478
2542
 
2479
2543
  ## For AI Coding Agents
2480
2544
 
2481
- This SDK is designed to be discovered and used by AI coding agents (Cursor, Copilot, Claude Code, etc.). The following files ship with the package to help agents understand and integrate the SDK without reading the full README:
2545
+ This SDK is designed to be discovered and used by any AI coding agent Cursor, Claude Code, Windsurf, Copilot, Codex, Cline, or any agent that can read files.
2546
+
2547
+ **The fastest path:**
2548
+
2549
+ ```bash
2550
+ npm install @amplitude/ai
2551
+ npx amplitude-ai
2552
+ ```
2553
+
2554
+ The CLI prints a prompt to paste into your agent:
2555
+
2556
+ > Instrument this app with @amplitude/ai. Follow node_modules/@amplitude/ai/amplitude-ai.md
2557
+
2558
+ The agent reads the guide, scans your project, and instruments everything in 4 phases: Detect, Discover, Instrument, Verify.
2559
+
2560
+ **Files shipped with the package:**
2482
2561
 
2483
- | File | Purpose |
2484
- | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------- |
2485
- | `AGENTS.md` | Machine-readable decision tree, canonical patterns, MCP surface, gotchas, and CLI reference |
2486
- | `llms.txt` | Compact discovery file listing tools, resources, and event names |
2487
- | `llms-full.txt` | Extended reference with full API signatures, provider coverage matrix, and common error resolutions |
2488
- | `mcp.schema.json` | Structured JSON describing the MCP server's tools, resources, and prompt |
2489
- | `.cursor/skills/instrument-with-amplitude-ai/SKILL.md` | Cursor skill that guides agents through instrumenting a project step by step |
2562
+ | File | Purpose |
2563
+ | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------- |
2564
+ | `amplitude-ai.md` | **Primary guide** self-contained 4-phase instrumentation workflow and full API reference |
2565
+ | `AGENTS.md` | Concise index with canonical patterns, MCP surface, gotchas, and CLI reference |
2566
+ | `llms.txt` | Compact discovery file listing tools, resources, and event names |
2567
+ | `llms-full.txt` | Extended reference with full API signatures, provider coverage matrix, and common error resolutions |
2568
+ | `mcp.schema.json` | Structured JSON describing the MCP server's tools, resources, and prompt |
2490
2569
 
2491
- Run `amplitude-ai mcp` to start the MCP server. Agents can call tools like `validate_file` to scan source code for uninstrumented LLM calls, or `suggest_instrumentation` for context-aware setup guidance based on the detected framework and provider.
2570
+ **Optional: MCP server for advanced tooling.** Run `amplitude-ai mcp` to start the MCP server (standard stdio protocol). MCP-compatible agents can call tools like `scan_project`, `instrument_file`, `validate_file`, and `generate_verify_test` for deeper analysis. The MCP server is not required for the core instrumentation workflow `amplitude-ai.md` is self-contained.
2492
2571
 
2493
2572
  ## For Python SDK Migrators
2494
2573