TokenPolice
Docs
Get startedWith your coding agent

Coding agent walkthrough

Say the word, answer a few questions, approve the plan, and run once while the agent verifies itself — with a full worked example.

Once the skill is installed, adding TokenPolice is four short touchpoints. Everything else — reading your code, writing the edits, checking the result — the agent does on its own.

We'll follow one real example throughout: a FastAPI support chat that calls OpenAI.

1. Say the word

Ask your agent, in plain language, to add TokenPolice:

You: Integrate TokenPolice into this app.

That's the whole trigger. Wordings like "add TokenPolice", "wrap my agent for TokenPolice", or "track and enforce LLM spend" work too — and the skill also wakes up on its own if you mention tp.init, tp.workflow, or TokenPoliceBlockedError.

The agent reads your repo first. For the support chat it comes back with something like:

Agent: Found a FastAPI app calling OpenAI from chat.py. It's a multi-turn support chat with a session_id per conversation and a user.id on the request. I'll wire TokenPolice around the chat handler. First I need a few things from you.

2. Answer its questions

The agent asks one short batch — nothing it could find in the code itself:

  • Your API key"Create one on the API Keys page (app.tokenpolice.ai/keys). It looks like tp_sk_... and is shown only once, so copy it when created. I'll put it in your environment, never in source." See Get your API key.
  • The collector URL — where usage metadata is sent. The default is https://collect.tokenpolice.ai; you only change it if you self-host or run a local stack.
  • Who your users are — which field is the user_id, which is the plan/tier, and which is the session_id. For the support chat the agent already spotted user.id and session_id and just confirms them.
  • Dry-run or enforce — it starts in dry-run by default: every call is recorded and you can see what would have been blocked, but nothing is ever blocked. Flipping to enforce later is a one-line change.
  • What a blocked call should return (HTTP apps only) — for the FastAPI app, 429 Too Many Requests or 402 Payment Required. Only relevant once you turn on enforce.

It also states the privacy terms up front: TokenPolice receives only token metadata — model, token counts, your chosen user_id / plan / session_id tags, and a content fingerprint used for loop detection. Raw prompt and completion text never leave your servers. See Data & privacy.

3. Approve the plan

Before touching a single file, the agent shows you exactly what it will change:

Agent: Here's the plan:

  • pip install token-police
  • Call tp.init(...) at startup in main.py, before the OpenAI client is imported, with firewall="dry_run".
  • Wrap the chat handler in chat.py with @tp.workflow, passing user_id=user.id, paid_plan=user.plan, and session_id=session_id.
  • Add TOKENPOLICE_API_KEY to your .env and .env.example.

Approve?

You reply yes or ask for a change. No code is edited until you approve.

4. Run your app once

The agent makes the edits, then asks you to run the app the way you normally would and send it a couple of messages — a normal request and a multi-turn conversation.

That run is the test. When you say you're done, the agent verifies itself: it checks that TokenPolice actually recorded your traffic (using the same key you already gave it — never your app, never another key) and reports the result:

Agent: Verified — status pass. TokenPolice recorded your calls with the right user and session tags. You're in dry-run mode, so nothing is blocked yet; flip firewall to "enforce" when you're ready. See your data at app.tokenpolice.ai.

You're done. Open app.tokenpolice.ai and your calls are there, with token counts and cost.

What "verified" means

The agent doesn't call the job done just because the code compiles. It confirms TokenPolice received real data from your run and that your identity tags came through — then reports pass, warn (with anything worth knowing), or fail. It won't stop on a fail.

After you're set up