Docs

Get started

Quickstart

Send one AI call through the Token Meter SDK and see it on your dashboard, tagged with the customer and feature it was for. You install two packages, create a key, and run one short script.

Before you start

  • A Token Meter account. To create a key you need to be an Owner, Admin or Team lead in your workspace.
  • An OpenAI API key (it starts with sk-). Create one in the OpenAI dashboard under API keys. The SDK measures calls you make with your own provider key, so OpenAI bills this test call to your OpenAI account. It costs a fraction of a cent.
  • Python 3.9 or later, or Node.js 20.6 or later. Pick one; this page shows both.
  • A terminal (Terminal on macOS, PowerShell on Windows), open in an empty folder.

Pick your language with the Python / TypeScript switch at the top of this page. Every code block follows it. The TypeScript code also runs as plain JavaScript.

1. Install

In your terminal, install the Token Meter SDK and the OpenAI SDK together. The Token Meter SDK does not include the OpenAI SDK; it wraps the one you install.

This guide uses OpenAI as the example. Anthropic, Gemini, Bedrock and Azure OpenAI work the same way; see Other providers under Moving it into your app.

terminal
npm install @tokenmeter/sdk openai
  • Python: the package is called tokenmeter-sdk, but in code you write import tokenmeter.
  • TypeScript and JavaScript: the SDK is an ES module only (the modern import syntax, not require). This page uses a .mjs file, which Node treats as an ES module. In your own project, add "type": "module" to package.json or use .mts files.

2. Create a key and save it

The SDK needs an ingest key (a password that lets your app send usage to Token Meter). Ingest keys start with tl_live_.

  1. In the Token Meter dashboard, open Governance › API keys and choose Create key.
  2. Enter a Name, for example quickstart. Environment is optional. If you are a Team lead, choose one of your teams under Team.
  3. Choose Create key. A box reading Key created: copy it now appears.
  4. Copy the key. It is shown once and never again. If you lose it, create a new one.
  5. In your terminal, save both keys as environment variables (settings your script reads, so the keys never sit in your code). Paste your real keys in place of the dots.
macOS / Linux
export TOKEN_METER_KEY="tl_live_..."
export OPENAI_API_KEY="sk-..."
Windows (PowerShell)
$env:TOKEN_METER_KEY = "tl_live_..."
$env:OPENAI_API_KEY = "sk-..."

These settings last until you close this terminal window. Run the script in the same window. For more on keys (environments, teams, rotating, revoking), see Ingest keys.

3. Write the script

Create a file in your folder with the name shown on the code block, and paste in the whole script.

first-call.mjs
import OpenAI from "openai";
import { TokenMeterClient, wrapOpenAI, action } from "@tokenmeter/sdk";

// Start Token Meter once, when your program starts.
const tl = new TokenMeterClient({ apiKey: process.env.TOKEN_METER_KEY ?? "" });

// Wrap the OpenAI client. It works exactly as before, and usage is now recorded.
const openai = wrapOpenAI(new OpenAI(), tl);

// Tag the call with the customer and the feature it was for.
const response = await action("generate-report", { customerRef: "acme", feature: "reports" }, () =>
  openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [{ role: "user", content: "Say hello in five words." }],
  }),
);

console.log(response.choices[0].message.content);

// Send the recorded usage now, before the script exits.
await tl.flush();

What each part does:

  • init / new TokenMeterClient starts Token Meter with your key. Do this once, when your program starts.
  • wrap / wrapOpenAI wraps the OpenAI client you already create. Every call works exactly as before. The SDK reads the token counts from OpenAI's reply after it arrives, so it adds no delay.
  • action tags every call made inside it. Here the tags say the call ran the generate-report action for customer acme in the reports feature. That is what lets the dashboard split cost by customer.
  • flush sends the recorded usage right away. A long-running app doesn't need this; a short script that exits straight after its call does.

4. Run it

In the same terminal window, run the script:

terminal
node first-call.mjs

You should see OpenAI's five-word reply printed in the terminal. If you see an error instead, check Troubleshooting.

5. Find it on the dashboard

  1. In the dashboard sidebar (Home view), choose Requests (/traces).
  2. Look at the top row of the Requests table. Within a few seconds it shows your call: Model gpt-4o-mini and Customer acme, plus its tokens, cost and latency. Reload the page if it isn't there yet. To see Feature reports, turn the Feature column on from the Columns menu.
  3. Choose Cost by action at the top of the Requests page to see the same call grouped under the generate-report action. This view updates about once a minute.

Team leads and Members see part of the traffic

Owners and Admins see every request. Team leads see only requests tagged with a team they lead; a key a Team lead creates always carries one of their teams, so its calls show up. Members see only requests tagged with their own email address. This applies to the Requests table; Cost by action is not filtered by role and covers actions from the whole workspace (the top 500).

Troubleshooting

What you seeWhat to do
ModuleNotFoundError: No module named 'openai' or Cannot find package 'openai'The OpenAI SDK isn't installed. Run the install command in step 1 again, in this folder.
KeyError: 'TOKEN_METER_KEY' (Python, when the script reads the variable with os.environ[...])The key isn't set in this terminal window. Set it again (step 2) and run the script in the same window. With SDK 0.2.2 or later, an empty key in TypeScript does not raise an error: the SDK prints a warning and your app runs without metering.
An OpenAI error saying OPENAI_API_KEY is missingSet OPENAI_API_KEY as shown in step 2.
await is only valid in async functions and the top level bodies of modules, or an error about require() of an ES moduleThe file is running as an old-style CommonJS script. Name it first-call.mjs, or add "type": "module" to package.json. Use import, not require.
The reply prints, but nothing appears on Requests after a minuteCheck the key. In Python, run python -c "import os, tokenmeter; tokenmeter.diagnose(os.environ['TOKEN_METER_KEY'])". It prints [tokenmeter diagnose] OK when the key works, or FAILED with the reason. In TypeScript, add onError: (err) => console.error(err) next to apiKey and run the script again.
FAILED: tokenmeter: invalid_key, or batch of 1 event(s) permanently rejectedThe key is wrong or was copied incompletely (invalid_key), or it was revoked or has expired (the TypeScript message covers both). Copy it again with no spaces, or create a new one at Governance › API keys.
FAILED: tokenmeter: key_revokedThe key was revoked or has expired. Create a new one at Governance › API keys.
could not reach https://ingest.timewastevc.comYour network blocks the ingest address. Allow outgoing HTTPS to ingest.timewastevc.com.

More on diagnose() in the reference.

Moving it into your app

  • Start once. Python keeps one Token Meter client for the whole process: call tokenmeter.init() once at startup. TypeScript has no global client: create one TokenMeterClient and pass it to every wrapper.
  • Other providers. In Python, tokenmeter.wrap() recognizes Anthropic, Gemini, Bedrock and Azure OpenAI clients too. In TypeScript, use wrapAnthropic, wrapGemini, wrapBedrock or wrapAzureOpenAI. Install that provider's SDK alongside, for example pip install "tokenmeter-sdk[anthropic]" or npm install @tokenmeter/sdk @anthropic-ai/sdk. See Providers. The Vercel AI SDK uses middleware instead; see Frameworks.
  • Flushing. A long-running app sends usage on its own, every 2 seconds or every 500 calls, whichever comes first. Short scripts should call tokenmeter.flush() or await tl.flush() before they exit. Serverless functions (AWS Lambda, Vercel, Cloudflare) need the pattern in Frameworks › Serverless, or usage recorded at the end of a request is lost.
  • Ingest address. You don't need to set one: the SDK sends to https://ingest.timewastevc.com. Pass endpoint only if Token Meter gives you a different ingest address. Python takes the address itself (endpoint="https://…"); TypeScript takes the full URL ending in /v1/events:batch.
  • Limits. The SDK is public, but it does nothing without a valid key. Plan limits are checked by Token Meter when usage arrives, not by the SDK.

Next steps

  • Attribution: tag calls with team, environment, project and member, and nest actions.
  • Providers: wrap Anthropic, Gemini, Bedrock and Azure OpenAI clients the same way.
  • Frameworks and runtimes: the Vercel AI SDK middleware, and flushing on serverless runtimes.
  • Router: meter OpenAI-compatible and Anthropic traffic by changing a base URL instead of code.