New to Metrx? This guide connects one agent, sends its LLM calls, and posts the outcome of one task.
1. Get an API key
Sign up at app.metrxbot.com and create a key in Settings → Security → API Keys. Keys start with sk_live_. Every snippet on this page reads the key from the METRX_API_KEY environment variable, so it never sits in your code.
2. Connect your agent
Pick the one that matches your stack. The SDKs and OpenTelemetry send telemetry from your own process: your LLM calls still go straight to your provider. The gateway proxy is the alternative for when you would rather change a base URL than add a package.
Python SDK
pip install "metrxbot[openai]>=0.4.0"
export METRX_API_KEY=sk_live_... # Settings -> Security -> API Keysimport os
from metrx import Metrx
from openai import OpenAI
m = Metrx(api_key=os.environ["METRX_API_KEY"], agent_key="refund-triage")
m.instrument() # patches OpenAI/Anthropic CLASSES: clients you create before or after are tracked
client = OpenAI()
with m.task(task_id="ticket-4821") as t: # any string id; non-UUID ids map to a deterministic UUIDv5 client-side
reply = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Summarise ticket 4821"}],
)
t.set_outcome(success=True)
# short scripts flush at exit automatically; on serverless call m.flush()The package installs as metrxbot and imports as metrx.
TypeScript SDK
npm install @metrxbot/sdk@0.7.0 @opentelemetry/api@1.9.1 @opentelemetry/sdk-trace-node@2.11.0 @opentelemetry/instrumentation@0.222.0 @opentelemetry/instrumentation-openai@0.20.0 openai@6.49.0Load this file before anything imports openai:
// metrx.ts: import this first, before anything imports "openai".
// Compile to CommonJS (tsc "module": "commonjs"). openai 7.x is not instrumented yet.
import { NodeTracerProvider, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { OpenAIInstrumentation } from '@opentelemetry/instrumentation-openai';
import { MetrxbotClient, MetrxbotSpanExporter, MetrxbotSpanProcessor } from '@metrxbot/sdk';
export const exporter = new MetrxbotSpanExporter({
apiKey: process.env.METRX_API_KEY!,
agentId: 'refund-triage',
});
const provider = new NodeTracerProvider({
spanProcessors: [new MetrxbotSpanProcessor(), new SimpleSpanProcessor(exporter)],
});
provider.register();
registerInstrumentations({ instrumentations: [new OpenAIInstrumentation()] });
export const metrx = new MetrxbotClient({ apiKey: process.env.METRX_API_KEY! });Then wrap each unit of work in task():
import { exporter, metrx } from './metrx';
import { task } from '@metrxbot/sdk';
import OpenAI from 'openai';
const client = new OpenAI();
export async function handleTicket() {
return task(metrx, async (t) => {
const reply = await client.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'Draft a reply to ticket 4821' }],
});
await exporter.forceFlush(); // send the span before the outcome
t.setOutcome({ success: true });
return reply;
});
}This setup is tested with CommonJS output and openai 6.x. With openai 7.x, or with native ES modules, the OpenAI instrumentation records no span yet, so nothing reaches Metrx.
OpenTelemetry
If your app already uses OpenTelemetry, point its OTLP/HTTP exporter at Metrx. For Node:
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="https://gateway.metrxbot.com/v1/traces"
export OTEL_EXPORTER_OTLP_PROTOCOL="http/json"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20${METRX_API_KEY}"
export OTEL_RESOURCE_ATTRIBUTES="service.name=refund-triage"
export OTEL_BSP_MAX_EXPORT_BATCH_SIZE="100"npm install @opentelemetry/api@1.9.1 @opentelemetry/auto-instrumentations-node@0.81.2 openai@6.49.0
node --require @opentelemetry/auto-instrumentations-node/register app.jsThe OpenAI instrumentation records spans for openai 4.19 to 6.x only. With openai 7 it records nothing and reports no error, so the install pins openai@6.49.0.
The Metrx receiver accepts OTLP/HTTP with JSON encoding only. Python’s OTLP exporter sends protobuf, so Python apps send through an OpenTelemetry Collector: see OpenTelemetry.
Gateway proxy (alternative)
Keep your OpenAI client and change its base URL. Your Metrx key authenticates with the gateway; your provider key goes in X-Provider-Key and is forwarded.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["METRX_API_KEY"], # Metrx key, sent as Authorization: Bearer
base_url="https://gateway.metrxbot.com/v1",
default_headers={
"X-Provider-Key": os.environ["OPENAI_API_KEY"],
"X-Agent-ID": "refund-triage",
},
)
reply = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello"}],
)import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.METRX_API_KEY, // Metrx key, sent as Authorization: Bearer
baseURL: 'https://gateway.metrxbot.com/v1',
defaultHeaders: {
'X-Provider-Key': process.env.OPENAI_API_KEY!,
'X-Agent-ID': 'refund-triage',
},
});
const reply = await client.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'Hello' }],
});3. Check the dashboard
Log into app.metrxbot.com to see the agent’s calls, cost by model, latency and error rates. If a request is refused, the response body names the problem: see Errors.
4. Next steps
- Tasks, outcomes, contracts and scorecards: how an outcome attaches to a task, and how a contract grades the agent.
- Integration Guide: headers, streaming, webhooks and migration from direct API calls.
- API Reference: every endpoint.
Pricing
Start free. See metrxbot.com/pricing for current plans.