Open History to see the last commit
1---
2title: Computer Use
3id: computer-use
4order: 1
5description: "Let the model use a web page for the user. It sees screenshots and sends clicks and key presses. Each conversation gets its own browser page, and the same code runs on OpenAI and Anthropic."
6keywords:
7 - tanstack ai
8 - computer use
9 - createComputerUse
10 - playwrightComputer
11 - browser agent
12 - playwright
13 - openai
14 - anthropic
15---
16
17`createComputerUse` gives `chat()` tools that click, type, and read a web page. The driver runs those actions in a browser.
18
19Computer use gives the model a screen, a mouse, and a keyboard. The model reads screenshots and sends clicks and key presses. Each conversation gets its own browser page, and the same code runs on OpenAI and on Anthropic.
20
21To build a full app step by step, with the screenshots in the chat, open the [Computer Use tutorial](../tutorials/computer-use).
22
23## 1. Install
24
25<!-- ::start:tabs variant="package-manager" mode="install" -->
26
27react: @tanstack/ai @tanstack/ai-react @tanstack/ai-openai @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
28vue: @tanstack/ai @tanstack/ai-vue @tanstack/ai-openai @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
29solid: @tanstack/ai @tanstack/ai-solid @tanstack/ai-openai @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
30svelte: @tanstack/ai @tanstack/ai-svelte @tanstack/ai-openai @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
31preact: @tanstack/ai @tanstack/ai-preact @tanstack/ai-openai @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
32angular: @tanstack/ai @tanstack/ai-angular @tanstack/ai-openai @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
33vanilla: @tanstack/ai @tanstack/ai-client @tanstack/ai-openai @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
34octane: @tanstack/ai @tanstack/ai-octane @tanstack/ai-openai @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod octane
35
36<!-- ::end:tabs -->
37
38Then download Chromium one time:
39
40```bash
41npx playwright-core install chromium
42```
43
44## 2. Give the model a browser
45
46Create the computer one time, at module scope. Then give its `tools`, `middleware`, and `interrupt` to `chat()`, together with the `threadId` of the request:
47
48```ts
49// app/api/chat/route.ts
50import { chat, chatParamsFromRequest, toServerSentEventsResponse } from '@tanstack/ai'
51import { openaiText } from '@tanstack/ai-openai'
52import { createComputerUse } from '@tanstack/ai-computer-use'
53import { playwrightComputer } from '@tanstack/ai-computer-playwright'
54
55const computerUse = createComputerUse({
56 driver: playwrightComputer({ startUrl: 'https://tanstack.com' }),
57})
58
59export async function POST(request: Request) {
60 const params = await chatParamsFromRequest(request)
61 const stream = chat({
62 adapter: openaiText('gpt-6.1-sol'),
63 messages: params.messages,
64 threadId: params.threadId,
65 runId: params.runId,
66 ...(params.parentRunId ? { parentRunId: params.parentRunId } : {}),
67 ...(params.resume ? { resume: params.resume } : {}),
68 tools: computerUse.tools,
69 middleware: [computerUse.middleware],
70 interrupts: [computerUse.interrupt],
71 })
72 return toServerSentEventsResponse(stream)
73}
74```
75
76> [!WARNING]
77> Run the browser in a network where it cannot reach private services. The browser can open every address that your server can reach. See [Keep the browser away from private addresses](#keep-the-browser-away-from-private-addresses).
78
79Does your agent run in a [harness](../harness/overview)? Use the `computerUse()` plugin with the same driver. It stores each screenshot in the media store of the session, and asks about safety checks through `permissions()`. See [Give your agent a computer](../harness/computer-use).
80
81## 3. Register the interrupt on the client
82
83Give `computerSafetyCheck` to `useChat`. It is the same object as `computerUse.interrupt`, and it does not load the driver or Playwright on the client.
84
85```tsx
86// app/chat.tsx
87import { useState } from 'react'
88import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
89import { computerSafetyCheck } from '@tanstack/ai-computer-use'
90
91export function Chat() {
92 const [input, setInput] = useState('')
93 const { messages, sendMessage } = useChat({
94 connection: fetchServerSentEvents('/api/chat'),
95 threadId: 'support-chat',
96 interrupts: [computerSafetyCheck],
97 })
98
99 return (
100 <>
101 {messages.map((message) => (
102 <div key={message.id}>
103 {message.parts.map((part, index) =>
104 part.type === 'text' ? <p key={index}>{part.content}</p> : null,
105 )}
106 </div>
107 ))}
108 <form
109 onSubmit={(event) => {
110 event.preventDefault()
111 sendMessage(input)
112 setInput('')
113 }}
114 >
115 <input value={input} onChange={(event) => setInput(event.target.value)} />
116 <button type="submit">Send</button>
117 </form>
118 </>
119 )
120}
121```
122
123To show an approval card when a safety check pauses the run, see [Safety checks](./safety-checks).
124
125## 4. Try it
126
127Start the app and send this message:
128
129```text
130Go to https://tanstack.com/ai and tell me the title of the page.
131```
132
133The model calls `navigate`, reads the screenshot, and answers in the chat. You now have a browser agent.
134
135To use Anthropic, change only the adapter:
136
137```ts
138import { anthropicText } from '@tanstack/ai-anthropic'
139
140const adapter = anthropicText('claude-opus-5-5')
141```
142
143## How it works
144
145`createComputerUse` gives you three things:
146
147- `tools`: the `computer` tool and the `navigate` tool. With `computer`, the model clicks, types, and takes screenshots. With `navigate`, it opens a URL.
148- `middleware`: stops the rest of a turn after a failed action. It also runs the OpenAI safety checks.
149- `interrupt`: asks a person to approve an action that has a safety check.
150
151Chromium starts on the first action. Each thread gets a new page on its own first action, and that page opens `startUrl`. After each action, the model gets a new screenshot.
152
153Do you need a full Linux desktop or your own computer, and not a browser page? See [Drivers](./drivers). For a machine that no driver controls, see [Custom driver](./custom-driver).
154
155## Advanced
156
157### One page for each thread
158
159Each `threadId` gets its own machine. With `playwrightComputer()`, the machine is a browser context with one page:
160
161- Each thread has its own cookies and storage.
162- Each thread has its own action queue and its own safety-check approvals.
163- All threads share one Chromium process.
164
165Give a stable `threadId` to `chat()`. If the id changes on each request, each request gets a new page.
166
167`useChat` sends the same thread id with each request of a chat. `chatParamsFromRequest` reads it, as in the route above.
168
169### Close the pages
170
171Each open page uses memory on your server. When a conversation ends, close the page of its thread. When the server stops, close all pages:
172
173```ts
174import { createComputerUse } from '@tanstack/ai-computer-use'
175import { playwrightComputer } from '@tanstack/ai-computer-playwright'
176
177declare const threadId: string
178
179const computerUse = createComputerUse({
180 driver: playwrightComputer({ startUrl: 'https://tanstack.com' }),
181 idleTimeout: 30 * 60_000,
182})
183
184// The conversation ended. Close the page of this thread.
185await computerUse.close(threadId)
186
187// The server stops. Close every page, then Chromium.
188await computerUse.close()
189```
190
191A thread that stays idle closes by itself. `idleTimeout` sets this time in ms:
192
193- The default is 10 minutes.
194- When the time ends, the page of the thread closes. The next action of that thread opens a new page at `startUrl`.
195- `false` keeps every thread open until you call `close`.
196
197### Keep the browser away from private addresses
198
199The browser can open every address that your server can reach. For example, a page can link to an internal admin page or to a cloud metadata URL.
200
201The `navigate` tool accepts only `http:` and `https:` URLs. It does not block private addresses.
202
203To keep the machine off your network, use a [sandbox driver](./drivers#linux-desktop-in-a-sandbox), for example E2B.
204
205### What the model can do
206
207The model clicks, double-clicks, drags, moves the mouse, scrolls, types text, and presses key combos. It can also wait.
208
209- A normal action returns a screenshot 500 ms later, so the page can settle. An OpenAI batch returns one screenshot after the last action.
210- `zoom` returns an image of one region. `cursor_position` returns the mouse position as text. Neither takes another screenshot.
211- `navigate` opens a URL, then returns a screenshot.
212
213To change the delay, set `screenshotDelay` in ms:
214
215```ts
216import { createComputerUse } from '@tanstack/ai-computer-use'
217import { playwrightComputer } from '@tanstack/ai-computer-playwright'
218
219const computerUse = createComputerUse({
220 driver: playwrightComputer({ startUrl: 'https://tanstack.com' }),
221 screenshotDelay: 1000,
222})
223```
224
225### Order and failures
226
227In one thread, actions run one at a time, in the order that the model calls them.
228
229- A driver factory, like `playwrightComputer()`, gives each thread its own machine. Those threads can run at the same time.
230- A driver that you pass in is one machine. Actions on that machine run one at a time, even from two threads.
231
232If one action fails, the other actions of that model turn do not run. The model gets this result for each of them:
233
234```text
235Not executed: an earlier computer action in this turn failed.
236```
237
238The next model turn starts clean. The model sees the error and can try again.
239
240### Limits
241
242- **One server process.** The pages live in the memory of one process. If the next request of a thread goes to another server, the thread gets a new page there.
243- **Time to approve.** If a person answers a safety check after `idleTimeout`, the page is already closed. The approved action then runs on a new page. Set `idleTimeout` above the time that a person takes to answer.
244
245### Model support
246
247- A model with no computer use is a type error at `chat()`.
248- The Anthropic adapter picks the newest computer tool version that the model supports. If the model supports no version, the adapter throws an error.
249- `openaiText` uses the Responses API, which has the computer tool. The Chat Completions adapters throw an error, because that API has no computer tool.