Get started
Reliability
A metering library that breaks your app or slows a real call is worse than none. This page states what the SDK promises, how your usage reaches Token Meter, and what can still go wrong.
Before you start
- There is nothing to set up. This page describes what the SDK does on its own.
- It covers the Python SDK (
tokenmeter-sdk) and the TypeScript SDK (@tokenmeter/sdk), version 0.2.2. Where the two behave differently, it says so.
The guarantees
- Never throws into your code during a call. Only the SDK's own record-keeping is wrapped to catch errors. The real provider call, and any error it raises, reaches your code unchanged. Tests cover this for the sending code, the buffer, and every wrapper's success, error and streaming paths. Two setup mistakes do raise an error, on purpose, at startup: Python's
tokenmeter.init()raises if a governance option has a value it doesn't recognize, and, if you use governance preflight, TypeScript'snew GovernanceClient()throws ifapiKeyorrouterUrlis empty. An empty or missing key does not raise (SDK 0.2.2 or later): the SDK prints a warning and your app runs without metering. - Never adds network time. Usage is read from the reply the provider already sent. The SDK sends nothing over the network while your call runs. Two exceptions do a little extra work in memory, never a network hop:
- Bedrock
InvokeModel: the response body is read in memory to find the usage, then handed back as a fresh stream, so your ownresponse["body"].read()still works. - Optional
optimizationfeatures you turn on (topicClassifier/topic_classifier,promptCaching/prompt_caching) run in your process just before the request. A topic classifier that throws is ignored and the call goes ahead.
- Bedrock
- Fails open. If Token Meter can't be reached, is rate-limiting you, or returns an error, your provider call is not affected. It has already succeeded or failed on its own terms.
Governance preflight is the opt-in exception
These guarantees cover metering. If you turn on governance preflight (tokenmeter.governed() in Python, GovernanceClient.guard() in TypeScript), that step is on the call path on purpose. It asks the Router whether the call is allowed before your provider call runs, with a 2-second timeout by default. In enforce mode (the default) a denial raises an error. If the Router can't be reached, it raises an error too, unless you set the fail mode to open:
- Python:
tokenmeter.init(ingest_key=..., router_url="https://router.timewastevc.com", governance_fail_mode="open"). Withoutrouter_url, preflight is not set up and counts as unreachable. - TypeScript:
new GovernanceClient({ apiKey, routerUrl: "https://router.timewastevc.com", failMode: "open" }).new GovernanceClient()throws at once ifapiKeyorrouterUrlis empty, so check the key is set before you build it.
A wrong or revoked key is not an outage. A 401 invalid_key, 403 key_revoked or 400 from preflight raises GovernanceRejected (Python) / GovernanceRejectedError (TypeScript), whatever the fail mode, so fail mode open never hides a bad key.
Delivery model
Each call's usage goes into a buffer in memory (a waiting list that holds 10,000 calls by default). Recording it is instant and never blocks your call. The SDK sends what is waiting every 2 seconds, or as soon as 500 calls are waiting, whichever comes first.
- When the buffer is full, the oldest call is dropped. Recent activity is what you are most likely to be looking at. The SDK counts every drop and reports the count with its next batch that Token Meter accepts. You see it in the Dropped column of the SDK fleet card on the Data health Pipeline view (
/data-health?tab=pipeline: open Data health from the coverage line in the account menu, or from Settings › Workspace, then choose the Pipeline link at the foot of Readiness), one row per SDK version and key. (On a narrow screen the column is hidden; widen the window.) While nothing is getting through, the count waits in the SDK and arrives with the first accepted batch. - Sent at least once, stored once. If a send fails or times out, the batch is sent again, so Token Meter may receive it twice. Every call carries its own id, made by the SDK, and Token Meter stores each id only once. A resent batch never counts spend twice.
- Retries. After a network error, a server error or a rate limit, the batch goes back in the buffer and is retried. The SDK waits longer after each failure (1 second, doubling up to 60 seconds) and follows the server's
Retry-Afteron a rate limit. - Batches that can never succeed are not retried. A bad or revoked key (
401,403) or a malformed batch (400) drops the batch. The TypeScript SDK reports it through itsonErroroption. The Python SDK prints a warning naming the key and counts the dropped calls. A batch too large in bytes (413) is dropped by the TypeScript SDK and split and sent again by the Python SDK. - Large backlogs go out in pieces. Token Meter accepts at most 1,000 calls in one batch. Both SDKs send a backlog as several batches, so a pile-up after an outage drains on its own.
Known failure modes
A stream that stops before its usage arrives
Providers send token counts at the end of a stream. If a stream is cut off before then, or a reply carries no usage at all, the call is still recorded with no token counts (Requests shows 0). On Requests (/traces), turn on the Usage column from the Columns menu: it says partial. Token Meter never guesses the numbers from the part of the stream it saw: a confident wrong number is worse than an honest gap.Serverless functions
On a platform that freezes your function between requests (AWS Lambda, Vercel, Cloudflare), usage recorded after the lastflush() or waitUntil() of a request is lost. No background timer runs to catch it. See Frameworks › Serverless for the call pattern that avoids this.When a normal process exits, the SDK makes one last attempt to send what is waiting, for up to 2 seconds. That covers a normal shutdown. It does not run if the process is killed, and in Node it does not run if your code calls process.exit() or crashes. If your code calls process.exit(), run await tl.flush() first. From SDK 0.2.2, tokenmeter.shutdown() (Python) and await tl.shutdown() (TypeScript) flush and stop the background sender.