Bring your own tokens

ready…
osebo46d ago

Right now everyone who uses this app is using my tokens from my api. How can I allow people to bring their own tokens from their Claude account?

5 repliesoldestnewest
Osebotfable-5-146d ago

Where your key is spent today: lib/agent/launch.ts copies the server's ANTHROPIC_API_KEY into every sandbox, and lib/agent/runner.mjs (Agent SDK) just reads it from env. That's ~all the spend; the only other callers are the Haiku decider (lib/decide.ts, triage/rerank) and the rolling thread summary (finish.ts) — tiny, and they'll stay on the server key.

Plan — per-user Anthropic credentials, resolved at launch

  1. Store: users.anthropic_key (AES-256-GCM encrypted at rest with a key derived from CREDENTIALS_SECRET ?? AUTH_SECRET), plus a …_hint (last 4 chars) and timestamp. Migration drizzle/0011.
  2. Settings page /settings (new; link in the header user menu): paste a key, we verify it against GET /v1/models before saving, show "sk-…abcd, added <date>", one-tap remove. Accepts a Console API key (sk-ant-api…) and a Claude Code OAuth token from claude setup-token (sk-ant-oat01-…) — the latter maps to CLAUDE_CODE_OAUTH_TOKEN in the sandbox. ⚠️ Anthropic's terms say Pro/Max subscription tokens are for Claude Code itself, not third-party products via the Agent SDK — I'll support the mechanism but flag it in the UI; you can disable it if you'd rather be strict.
  3. Resolution order per run (new lib/anthropic-keys.ts, used by launch.ts and the local worker/index.ts): the person who triggered the run → the workspace owner → the server's ANTHROPIC_API_KEY. The job log records which one was used ("billing: @mira's key"). Guest-triggered runs can only land on owner/server.
  4. Opt-out of the shared key: env REQUIRE_OWN_ANTHROPIC_KEY=1 drops step 3's server fallback; Ask Claude / run / fork then 403 with "add your Anthropic key in Settings" instead of launching a run that would crash.
  5. Composer shows a small note under Ask Claude saying whose key the run will use, so nobody is surprised.

Not doing: per-run spend tracking, key rotation UI, keys for the decider/summaries.

Osebotfable-5-146d agothreadide@8efad4c

Answer: the only place your key was being spent on other people was lib/agent/launch.ts, which copied the server's ANTHROPIC_API_KEY into every sandbox. Now each run resolves whose key to use, and users can add their own.

What shipped (8efad4c, pushed to thread/9cb287a5)

  • /settings (link next to your name in the header): paste an Anthropic key. API keys are verified with GET /v1/models (no tokens spent) before saving, stored AES-256-GCM encrypted under CREDENTIALS_SECRET ?? AUTH_SECRET, and only ever shown back as sk-ant-api03…k9Qz. Two-tap remove.
  • Per run (lib/anthropic-keys.ts): person who asked → workspace owner → server ANTHROPIC_API_KEY. The job log gets a line like billing: API key of @mira (who asked) so any bill is traceable. Same in the local worker.
  • Composer shows "Runs on your Anthropic key." / "Runs on @osebo's key (workspace owner) — add your own in Settings" under the row while Ask Claude is ticked.
  • Fail early: Ask Claude / Run / Fork / new thread return 403 "add your key in Settings" when there's nothing to bill, instead of launching a sandbox that crashes.
  • Migration drizzle/0011 (3 nullable columns on users) runs on the next prod build.

Two decisions for you, osebo

  1. Is the server key still lent out? Default: yes, as the last fallback — so nothing changes for anyone until they add a key. Set REQUIRE_OWN_ANTHROPIC_KEY=1 on Vercel and your key is only used for the Haiku decider + thread summaries (cents); everyone else must bring their own or be in a workspace whose owner has one.
  2. Claude Code subscription tokens. claude setup-token gives a sk-ant-oat01-… token; the form accepts it and the sandbox gets it as CLAUDE_CODE_OAUTH_TOKEN, which the Agent SDK honours. But Anthropic's terms say Pro/Max tokens are for Claude Code itself, not third-party products, so the form warns inline. If you'd rather be strict, delete the oauth branch in classifyKey — one line.

Verified / not verified

  • tsc clean; seal/open round-trip + wrong secret + tampering, and key classification exercised with a tsx script.
  • UI from static mockups over the real CSS (screenshot attached); the app can't run in the sandbox, so the end-to-end save → run path is untested until the preview deploy.
/settings with a saved key (static mockup over the real globals.css; the app can't run in the sandbox)
/settings with a saved key (static mockup over the real globals.css; the app can't run in the sandbox)
oseboasked Osebot46d ago

How can we allow people to just use their Claude subscription?

Osebotfable-5-146d ago

Short answer: the mechanism for subscriptions already shipped in 8efad4c — a user runs claude setup-token in their terminal (needs Claude Code installed and logged in with their Pro/Max account), gets an sk-ant-oat01-… token, pastes it on /settings, and their runs go out with CLAUDE_CODE_OAUTH_TOKEN instead of an API key. The Agent SDK honours it, so the sandbox bills their subscription.

What we can't do: a browser "Sign in with Claude" button. Anthropic has no public OAuth client for third-party apps to bill a claude.ai subscription; the only client is Claude Code's own, and reusing its client id in another product is against the consumer terms (Anthropic has actively cut off tools that did that). So the paste-a-token route is the only one available, and even it sits in a grey area that the UI should state plainly rather than hide behind a warning that appears only after pasting.

What I'll change now (small):

  1. /settings gets two explicit tabs/sections — API key (Console, pay-as-you-go) and Claude subscription (step-by-step: install Claude Code → claude setup-token → paste) — instead of one field with a post-hoc warning. Same storage, same PUT /api/me/anthropic-key.
  2. Status line says "Claude subscription token" and the composer note says "Runs on your Claude subscription" when that's what's saved.
  3. README gains a "Using a Claude subscription" paragraph with the caveat.

Then push + refresh the PR description. I'll also merge main (sticky header) into the branch so the PR stays mergeable.

Osebotfable-5-146d agothreadide@1bc941c

How people use their Claude subscription

It already works on this branch — the gap was that nobody could tell. Here's the whole picture:

The only route Anthropic leaves open

  1. User installs Claude Code (npm i -g @anthropic-ai/claude-code), runs claude once and signs in with their claude.ai (Pro/Max) account.
  2. Runs claude setup-token → browser approval → it prints a long-lived token sk-ant-oat01-….
  3. Pastes it on /settings. We store it encrypted; the sandbox gets it as CLAUDE_CODE_OAUTH_TOKEN, which the Agent SDK uses instead of an API key, so the run bills their subscription.

What we can't build: a "Sign in with Claude" button

There is no OAuth client Anthropic gives to third-party apps for claude.ai subscriptions. The only client id that works is Claude Code's own, and using it from another product is a violation of the consumer terms — Anthropic has cut off tools that did exactly that. So a browser flow would be both unofficial and fragile. The paste-a-token route is the honest version of the same thing, and it still sits in a grey area: the terms say subscription access is for Claude Code itself. The settings page now says so plainly and lets people choose.

If you'd rather not allow subscriptions at all: delete the oauth branch in classifyKey (lib/anthropic-keys.ts) — one line, and the form will refuse those tokens.

What changed in 1bc941c

  • /settings has an API key | Claude subscription switch. The subscription side shows the three steps above plus the caveat, instead of a warning that only appeared after pasting. The switch is cosmetic: whatever you paste is classified by prefix, and a mismatch is flagged, not refused.
  • Status line, composer note ("Runs on your Claude subscription." / "Runs on @osebo's Claude subscription (workspace owner)…") and the job log line all say "Claude subscription" for these tokens (new credentialKind() helper).
  • README documents the route and the caveat.
  • Merged main (sticky header) into the branch so the PR stays clean.

Verified / not verified

  • tsc clean; layout checked on a static mockup over the real CSS (attached). The app can't run in the sandbox, so the subscription-token path through the Agent SDK is untested end-to-end — the first run with a setup-token token on the preview deploy is the real test. If it fails, the run log will show the SDK's auth error right after the billing: Claude subscription token of @… line.
  • Subscription tokens can't be verified on save (no cheap endpoint accepts them); a bad token surfaces as a failed run, and the form says so.
/settings in "Claude subscription" mode (static mockup over the real globals.css)
/settings in "Claude subscription" mode (static mockup over the real globals.css)