TokenSpy

Setup guide

Protect your app from surprise AI bills. Every step is below, with screenshots, exactly as you'll see it.

What you need

  • A TokenSpy account (free for your first app)
  • A free Cloudflare account (no card needed). You can create it when TokenSpy sends you there
  • Your AI provider key (OpenAI, Anthropic or Gemini)

Why Cloudflare?

The guard holds your AI key, so it runs in your own Cloudflare account, not on our servers. That way TokenSpy never sees or stores your key. We only receive usage numbers: cost, tokens, model and time.

  1. Step 1

    Sign up and pick your AI

    Create your free account on TokenSpy. The setup wizard opens by itself.

    Choose the AI your app uses (OpenAI, Anthropic or Google Gemini) and where your app lives (Vercel, Lovable, Bolt, Replit, Netlify or Not sure). Your progress is saved, so you can stop and come back.

    The setup wizard asking which AI you use and where your app lives
    The setup wizard asking which AI you use and where your app lives
  2. Step 2

    Put the guard in your Cloudflare

    Press Connect Cloudflare. Cloudflare's own page opens. Approve TokenSpy there. A free Cloudflare account is enough, and you can create one on that page.

    Back in TokenSpy, press Install my guard. A progress bar shows each step: finding your account, making your free workers.dev address if you don't have one, uploading the guard, switching it on and connecting it to TokenSpy.

    It costs nothing. TokenSpy uses this connection only to install and update your guard, never for your AI key. You can disconnect any time in Settings, under Connections. Your guard keeps running.

    Connect Cloudflare and Install my guard, with the progress bar
    Connect Cloudflare and Install my guard, with the progress bar

    Prefer a terminal? There is an advanced way. See step 7.

  3. Step 3

    Add your AI key on your guard's own page

    Press Add your AI key. It opens a page on your own guard (an address ending in workers.dev/setup), not on TokenSpy.

    Pick your AI, paste your key and press Save. The key goes straight from your browser to your guard. Your guard checks it with the AI company and keeps it. TokenSpy never sees it.

    The link works for 10 minutes. If it runs out, press the button again.

    Or do it in Cloudflare: open Workers & Pages, click your guard, go to Settings, then Variables and Secrets, press Add, choose the type Secret and name it OPENAI_API_KEY, ANTHROPIC_API_KEY or GEMINI_API_KEY.

    The Add your AI key button on the setup screen
    The Add your AI key button on the setup screen

    Best: make a new key just for the guard. Your bill still goes to the same account.

  4. Step 4

    Point your app at the guard

    Your app needs two settings. They use the names the official AI libraries read by themselves, so most apps need no code change. They are listed below. Use your own guard address.

    The guard password goes where your real AI key used to be. Find it in the Guard password box: press Show or Copy. You can look at it any time, or press Reset for a new one.

    On Vercel: press Connect Vercel, choose your project and press Fill in and redeploy. On Lovable, Bolt or Replit: copy the ready-made prompt and paste it into your builder's chat. Otherwise, add the two settings yourself.

    Then redeploy your app, or it keeps using the old settings.

    • OpenAI: OPENAI_BASE_URL = https://YOUR-GUARD.workers.dev/openai/v1 and OPENAI_API_KEY = your guard password
    • Anthropic: ANTHROPIC_BASE_URL = https://YOUR-GUARD.workers.dev/anthropic and ANTHROPIC_API_KEY = your guard password
    • Gemini: GOOGLE_GEMINI_BASE_URL = https://YOUR-GUARD.workers.dev/gemini and GEMINI_API_KEY = your guard password
    The two settings, the Connect Vercel button and the ready-made prompt
    The two settings, the Connect Vercel button and the ready-made prompt

    Not sure where your app keeps its settings? They are often called environment variables or secrets.

  5. Step 5

    Use your app's AI once

    The dashboard now shows Waiting for your app's first AI call…. Use your app's AI once, for example send one chat message. The screen turns green when the call arrives, or red with the reason.

    Nothing happening? Press Test my app. It sends one tiny real AI call through your guard (it costs a fraction of a cent) and checks 4 things:

    • Your guard is online
    • Your guard accepts the guard password
    • Your AI key works
    • TokenSpy received the report
    The waiting screen and the Test my app checks
    The waiting screen and the Test my app checks
  6. Step 6

    Check your limits

    Your limits are already filled in: $2 a day and $30 a month. So if your app goes crazy, the most it can spend is $2 a day. You can change them any time.

    On the AI Guard page you also find the kill switch, which stops all AI calls right away. Want an email before a limit is reached? Turn on alerts on the Alerts page.

  7. Step 7

    Advanced: set up with a terminal instead

    Only if you prefer a terminal. You need Node.js 18 or newer. On the AI Guard page, download the guard (.zip) and unzip it.

    npm install
    npm run setup

    Open the guard-worker folder, then open a terminal in it. On Windows: click the folder's address bar, type powershell and press Enter. On a Mac: right-click the folder, then New Terminal at Folder.

    Type npm install and press Enter. When it finishes, type npm run setup and press Enter.

    Setup asks for a setup code. Get one on the AI Guard page with Get a setup code. It has 12 letters and numbers, like ABCD-EFGH-JKLM, works once and lasts 10 minutes. Then setup signs you in to Cloudflare, asks for your AI key (hidden as you type, sent only to Cloudflare), uploads the guard and fills in its address on the dashboard by itself.

    If you see "You need a workers.dev subdomain", open dash.cloudflare.com, then Workers & Pages, once, and pick a name. Then run npm run setup again.

If something goes wrong

A red banner says my app's AI calls are being refused
Your app reaches the guard, but the guard says no. The banner tells you why and has a button that fixes it. The reasons are listed below.
guard_password_missing
Your app didn't send the guard password. Copy it from the Guard password box into your app, where the AI key goes, then redeploy.
guard_password_wrong
Your app sent a wrong or old guard password. Copy the current one from the Guard password box into your app, then redeploy.
guard_password_not_set
Your guard has no password yet, so it refuses every call. Press Create guard password on the AI Guard page, put it in your app, then redeploy.
ai_key_missing
Your guard has no AI key for the AI your app uses. Press Add your AI key and save it.
ai_key_rejected
The AI company refused the key on your guard: it is wrong, deleted or out of credit. Add a working key.
My app gets 429 or 503
That's the guard working. A limit was reached (429) or the kill switch is on (503). Check the AI Guard page.
Test my app says the password isn't accepted
Your guard may be older than version 0.4.0. Update it (see the last question below). Or you just reset the password: wait 30 seconds and test again.
Cloudflare says I need a workers.dev subdomain
With the terminal way, open Cloudflare, go to Workers & Pages once and pick a name. Then run npm run setup again.
I want to stop using the guard
Put your real AI key back in your app, where the guard password is. Delete the guard address setting (for example OPENAI_BASE_URL). Redeploy. The guard then gets no calls, and you can delete it in Cloudflare any time.
How do I update my guard?
When a new version is out, the AI Guard page shows Update available. If Cloudflare is connected, press Update my guard now. If not, download the guard again and run npm run update -- --name your-guard-name in the guard-worker folder. Your password, AI keys and settings stay the same.