# Cloudflare Agents

Render a Cloudflare Agent conversation with its existing connection and durable history.

## Connect the Cloudflare chat hook

Add the tested Cloudflare packages and their required peers.

```bash
npm install agents@0.32.0 @cloudflare/ai-chat@0.12.2
```

Set up the server-side chat Agent using the [Cloudflare Agents guide](https://developers.cloudflare.com/agents/api-reference/agents-api/). Ghost reads the existing client hook. Follow [view setup](/libraries/ai#installation) for styles and dependencies.

```tsx
import { useState } from "react";
import { Button } from "@collectivecreation/ghost-ui/components/button";
import { useAgent } from "agents/react";
import { useAgentChat } from "@cloudflare/ai-chat/react";
import { createAiSdkAdapter } from "@collectivecreation/ghost-ui/chat-adapter-ai-sdk";
import { useChatAdapter } from "@collectivecreation/ghost-ui/chat-runtime-react";
import { Message, MessageBody } from "@collectivecreation/ghost-ui/components/message";
import { MessageParts } from "@collectivecreation/ghost-ui/components/message-parts";
const adapter = createAiSdkAdapter();
export function CloudflareConversation({ agentClass, name, revision }: { agentClass: string; name: string; revision: number }) {
  const agent = useAgent({ agent: agentClass, name });
  const chat = useAgentChat({ agent });
  const runtime = useChatAdapter(adapter, {
    ...chat,
    status: chat.isRecovering ? "submitted" : chat.isStreaming ? "streaming" : chat.status,
    error: chat.error ?? chat.connectionError ?? undefined,
  }, { identity: { threadId: name, revision } });
  const [error, setError] = useState<unknown>();
  return <>
    {runtime.messages.map(message => <Message key={message.id} actor={message.actor}><MessageBody>
      <MessageParts parts={message.parts} />
    </MessageBody></Message>)}
    <Button disabled={runtime.capabilities.send.support !== "supported" || !runtime.capabilities.send.available}
      onClick={() => { setError(undefined); void Promise.resolve().then(() => runtime.controller.send({ text: "Review the brief", attachments: [] })).catch(setError); }}>Review brief</Button>
    {runtime.capabilities.stop.support === "supported" && runtime.capabilities.stop.available &&
      <Button onClick={() => { void Promise.resolve().then(() => runtime.controller.stop?.()).catch(setError); }}>Stop</Button>}
    {(error || runtime.controller.error) ? <p role="alert">The request could not complete.</p> : null}
  </>;
}

```

isStreaming includes server-pushed continuations; isRecovering reports a durable turn being restored. Using only the underlying AI SDK status would miss those states. The native stop implementation also aborts tool continuations; retain it when binding the hook. Compose input/actions with runtime.controller and inspect current capabilities.

## Server state and tools

Cloudflare owns websocket connection/recovery, durable turns and native message synchronization. Read application Agent state through the native connection and validate it for your UI. Browser tool callbacks and addToolOutput are application execution paths; do not connect arbitrary tool data to privileged execution. Approval acknowledgement remains distinct from a tool result.

## Troubleshooting

If the view appears frozen, check whether the connection is recovering. A server turn can still be busy while the client is idle. Keep the hook’s connection and lifecycle state available, and replace the hook when the [thread changes](/libraries/ai-threads).
