> ## Documentation Index
> Fetch the complete documentation index at: https://docs.relayapp.im/llms.txt
> Use this file to discover all available pages before exploring further.

# Cloudflare Think

> Connect Cloudflare Think to receive Relay messages and send replies.

Use the local signed forwarder with `wrangler dev` on your laptop, then use a webhook subscription for the deployed Worker.

## Before you start

* Node.js `22.22.3` or newer.
* A Cloudflare account with Workers AI.
* A staging agent, Agent Token, and webhook signing secret.

## Connect

### On your laptop

Clone the starter, install it, and start the local Worker:

```bash theme={null}
git clone --branch main https://github.com/RelayMessenger/Relay-SDK.git
cd Relay-SDK/cookbook/cloudflare-think-agent
npm ci
cp .dev.vars.example .dev.vars
npm run dev
```

Run the forwarder in a second terminal:

```bash theme={null}
npx relaymessenger listen --forward-to http://localhost:8787/webhooks/relay
```

Set the printed local signing secret as `RELAY_WEBHOOK_SECRET` in the Worker. The Worker verifies the signed request, acknowledges it with `202`, and runs the Think turn after receipt.

### Deployed

Deploy the Worker and create a [webhook subscription](/webhooks/subscriptions) for its public `/webhooks/relay` route. Store the subscription signing secret and Agent Token in the deployed Worker secrets. The deployed path uses the same handler and signature verification as local forwarding.

The source is [cookbook/cloudflare-think-agent](https://github.com/RelayMessenger/Relay-SDK/tree/main/cookbook/cloudflare-think-agent). Its non-secret staging settings use `https://api.relayapp.im` as the API origin, the agent Handle, and the Workers AI model ID.

## Send it a message

Open Relay on your phone and message the agent's Handle. The local Worker or deployed Worker receives the event, Think runs the model, and the reply returns in the same Chat.

## What it can do

| Relay feature      | Cloudflare Think starter                                        |
| ------------------ | --------------------------------------------------------------- |
| Receive a Message  | Yes, through signed local forwarding or a webhook subscription. |
| Reply in the Chat  | Yes, one complete idempotent Message per model turn.            |
| Send attachments   | Yes, through the Chat SDK adapter it wraps.                     |
| Read Chat history  | Yes, through the adapter's history walk.                        |
| Mark the Chat read | Yes, on receipt.                                                |

Think uses one durable root state per Relay Chat. Its current burst window is 600 ms, and a newer Message does not cancel a running turn.

## When it fails

* Signature verification fails: the secret does not belong to this delivery path, or the request body changed.
* No reply arrives: read the Worker logs to find whether the turn or model call failed.
* A send result is uncertain: retry with the same idempotency key and content.

## Next steps

* [Verify webhook signatures](/webhooks/verify-signatures)
* [Manage webhook subscriptions](/webhooks/subscriptions)
* [Handle delivery retries](/webhooks/delivery)
