JavaScript SDK
Installation
npm install agentmetrics
Requires Node.js 18 or later. Works with TypeScript, ESM, and CommonJS.
Configuration
Call configure() once at startup, before any tracking calls.
import agentmetrics from "agentmetrics";
agentmetrics.configure({ apiKey: process.env.AGENTMETRICS_API_KEY });
Note
configure() must be called before any track() wrappers execute. Call it at module load time, not inside a function.
Tracking an agent
agentmetrics.track()
Wrap your agent function. Every call is tracked automatically.
const myAgent = agentmetrics.track("my-agent", async (task) => {
const answer = await askLLM(task);
return answer;
});
const result = await myAgent(task);
The wrapper records start time, end time, success/failure, and error messages. Errors are re-thrown after recording.
Signature
agentmetrics.track(
agentId: string,
fn: (...args: any[]) => Promise<any>,
options?: TrackOptions
): (...args: any[]) => Promise<any>
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
agentId | string | Yes | Identifier shown in the dashboard. |
fn | function | Yes | Your async agent function. |
options.metadata | object | No | Key-value pairs attached to every run. |
Tracking steps
agentmetrics.step()
Times a named phase within a tracked agent. Each step appears with its own latency in the dashboard.
const run = agentmetrics.track("pipeline", async (query) => {
const docs = await agentmetrics.step("retrieve", () => vectorSearch(query));
const answer = await agentmetrics.step("generate", () => callLlm(query, docs));
return answer;
});
Tracking tools
agentmetrics.tool()
Times an individual tool call within a tracked agent.
const run = agentmetrics.track("research-agent", async (query) => {
const results = await agentmetrics.tool("web_search", () => webSearch(query));
const output = await agentmetrics.tool("code_interpreter", () => runCode(results));
return summarize(output);
});
Eval scores
agentmetrics.score()
Attach named numeric scores to the current run. Call inside any tracked function. Scores appear in the dashboard alongside latency and token data.
const run = agentmetrics.track("rag-agent", async (query) => {
const answer = await generateAnswer(query);
agentmetrics.score("relevance", await evaluateRelevance(query, answer));
agentmetrics.score("groundedness", await evaluateGroundedness(answer));
return answer;
});
Note
score() must be called inside a track()-wrapped function. Calls outside a tracked run log a warning and are ignored.
Subagent tracking
When a track()-wrapped function calls another track()-wrapped function, the inner run is automatically linked to the outer run via parent_trace_id. No extra configuration is needed.
const subagent = agentmetrics.track("subagent", async (task) => {
return await callLlm(task);
});
const orchestrator = agentmetrics.track("orchestrator", async (tasks) => {
return await Promise.all(tasks.map(subagent));
});
Each subagent run appears as a child of the orchestrator run in the dashboard, with its own tokens, latency, and status.
Getting the trace ID
agentmetrics.traceId
Returns the active trace ID from inside a tracked function. Use it to correlate AgentMetrics runs with your own logs.
const run = agentmetrics.track("my-agent", async (task) => {
console.log("trace_id:", agentmetrics.traceId);
return await askLLM(task);
});
Returns undefined when accessed outside a tracked function.
Auto-capturing token usage
agentmetrics.instrument()
Call instrument() once after configure(). It patches the OpenAI and Anthropic Node.js SDKs to automatically capture token counts and model names on every LLM call within a tracked run.
agentmetrics.configure({ apiKey: process.env.AGENTMETRICS_API_KEY });
agentmetrics.instrument();
No changes to your existing openai.chat.completions.create() or anthropic.messages.create() calls are needed.
TypeScript
The SDK ships with full TypeScript types.
import agentmetrics, { type TrackOptions } from "agentmetrics";
const options: TrackOptions = { metadata: { env: "production" } };
const myAgent = agentmetrics.track(
"my-agent",
async (task: string): Promise<string> => {
const answer = await askLLM(task);
return answer;
},
options
);
CommonJS
const agentmetrics = require("agentmetrics");
agentmetrics.configure({ apiKey: process.env.AGENTMETRICS_API_KEY });
agentmetrics.instrument();
const myAgent = agentmetrics.track("my-agent", async (task) => {
return await askLLM(task);
});
Error handling
Errors thrown inside a tracked function are re-thrown after AgentMetrics records them. No wrapping or try/catch needed.
const myAgent = agentmetrics.track("my-agent", async (task) => {
throw new Error("something went wrong"); // tracked as failure, then re-thrown
});