# Set up Griffin

You are a coding agent: Freebuff, Claude Code, Codex, Cursor or any other. Griffin (https://griffin.app) shows what each
AI call in this app costs, by feature, then moves calls to cheaper models that pass the app's own replays. You wrap the
app's AI clients with the Griffin SDK, a `fetch` wrapper, and get the developer a key. Do the steps in order. The app
must work exactly as before.

## 1. Get a key: the agent link (before any code)

Run this, then open its `url` in the developer's browser yourself (`open` on macOS, `xdg-open` on Linux, `start` on
Windows) and show them its `user_code`:

```sh
curl -sX POST 'https://griffin.app/api/agent/connect'
```

They sign up or sign in (free: Google, Apple or email) and press Approve. Meanwhile, poll every 5 seconds with the
`device_code` it gave you:

```sh
curl -s https://griffin.app/api/agent/poll -d device_code=<device_code>
```

- `pending`: keep polling. `slow_down`: wait longer. `expired`: start again (a link lasts 10 minutes).
- `approved`: save `api_key` as `GRIFFIN_KEY` in the project's `.env` (check `.gitignore` covers `.env` first) or the
  host's secret store. Never print, log or commit it.
- No approval after 10 minutes: go on without a key. Step 4 passes without one. Tell the developer they can run the
  link again later.

Don't run `npx @winding-labs/griffin` from your shell. That is for a person at a terminal, and it waits up to 15
minutes for a browser sign-in.

## 2. Install the SDK

Use the project's package manager: `npm i @winding-labs/griffin` (or `pnpm add`, `yarn add`, `bun add`). It has no
dependencies and runs on Node 18+, Bun, Deno, Cloudflare Workers and Vercel Edge. There is no SDK for Python or other
languages yet: leave that code as it is and tell the developer.

## 3. Wrap each AI client

Find every AI call: OpenRouter, OpenAI, Anthropic, Gemini, Groq, Together, Mistral, DeepSeek, xAI, Fireworks, Router.com,
Vercel or Cloudflare AI Gateway, through their SDKs, the Vercel AI SDK or raw `fetch`. Give each product feature one
short kebab-case use case (`chat`, `review`, `summarize`). Add one line per client:

```ts
import { griffin } from '@winding-labs/griffin';

new OpenAI({ ...options, fetch: griffin({ useCase: 'chat' }) }); // OpenRouter through baseURL too
new Anthropic({ ...options, fetch: griffin({ useCase: 'review' }) });
createOpenAI({ ...options, fetch: griffin({ useCase: 'summarize' }) }); // any AI SDK provider
const aiFetch = griffin({ useCase: 'review' }); // raw fetch: aiFetch(url, init)
```

- Cloudflare Workers: make it inside the handler, `griffin({ key: env.GRIFFIN_KEY, useCase: 'review' })`. It keeps its
  report alive after the handler returns by itself (SDK 0.6.3+), in requests, queues and crons.
- Scripts and serverless handlers without `waitUntil`: `await griffin.flush()` before they return.
- Change nothing else: models, prompts, parameters, headers, retries and response handling stay as they are.
- Never make the key required: no `process.env.GRIFFIN_KEY!`, no throw or exit when it's missing, no startup call to
  griffin.app, no try/catch around `griffin()`. Leave test mocks alone.

## 4. Verify (no key needed)

Without `GRIFFIN_KEY` the wrapper is plain `fetch`: each call goes to the provider unchanged and nothing goes to
Griffin. So:

1. Typecheck, lint, build and tests pass unchanged, with or without a key.
2. One real AI call, made the way the app already makes it, answers as before. With no key the SDK may log one line
   saying so. That's expected.
3. With `GRIFFIN_KEY` set, that call shows on the dashboard (the poll's `dashboard` link) within a minute.

A missing key never fails this step. The setup is done, and calls show up once the key is in place.

## 5. Tell the developer

Which calls you wrapped and their use cases, where `GRIFFIN_KEY` is set (or that it still needs one: run step 1 again,
or make one at https://griffin.app/start under Keys), and anything you skipped and why. Docs: https://griffin.app/docs/
