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
79## 3. Register the interrupt on the client
80
81Give `computerSafetyCheck` to `useChat`. It is the same object as `computerUse.interrupt`, and it does not load the driver or Playwright on the client.
82
83```tsx
84// app/chat.tsx
85import { useState } from 'react'
86import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
87import { computerSafetyCheck } from '@tanstack/ai-computer-use'
88
89export function Chat() {
90 const [input, setInput] = useState('')
91 const { messages, sendMessage } = useChat({
92 connection: fetchServerSentEvents('/api/chat'),
93 threadId: 'support-chat',
94 interrupts: [computerSafetyCheck],
95 })
96
97 return (
98 <>
99 {messages.map((message) => (
100 <div key={message.id}>
101 {message.parts.map((part, index) =>
102 part.type === 'text' ? <p key={index}>{part.content}</p> : null,
103 )}
104 </div>
105 ))}
106 <form
107 onSubmit={(event) => {
108 event.preventDefault()
109 sendMessage(input)
110 setInput('')
111 }}
112 >
113 <input value={input} onChange={(event) => setInput(event.target.value)} />
114 <button type="submit">Send</button>
115 </form>
116 </>
117 )
118}
119```
120
121To show an approval card when a safety check pauses the run, see [Safety checks](./safety-checks).
122
123## 4. Try it
124
125Start the app and send this message:
126
127```text
128Go to https://tanstack.com/ai and tell me the title of the page.
129```
130
131The model calls `navigate`, reads the screenshot, and answers in the chat. You now have a browser agent.
132
133To use Anthropic, change only the adapter:
134
135```ts
136import { anthropicText } from '@tanstack/ai-anthropic'
137
138const adapter = anthropicText('claude-opus-5-5')
139```
140
141## How it works
142
143`createComputerUse` gives you three things:
144
145- `tools`: the `computer` tool and the `navigate` tool. With `computer`, the model clicks, types, and takes screenshots. With `navigate`, it opens a URL.
146- `middleware`: stops the rest of a turn after a failed action. It also runs the OpenAI safety checks.
147- `interrupt`: asks a person to approve an action that has a safety check.
148
149Chromium 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.
150
151Do 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).
152
153## Advanced
154
155### One page for each thread
156
157Each `threadId` gets its own machine. With `playwrightComputer()`, the machine is a browser context with one page:
158
159- Each thread has its own cookies and storage.
160- Each thread has its own action queue and its own safety-check approvals.
161- All threads share one Chromium process.
162
163Give a stable `threadId` to `chat()`. If the id changes on each request, each request gets a new page.
164
165`useChat` sends the same thread id with each request of a chat. `chatParamsFromRequest` reads it, as in the route above.
166
167### Close the pages
168
169Each open page uses memory on your server. When a conversation ends, close the page of its thread. When the server stops, close all pages:
170
171```ts
172import { createComputerUse } from '@tanstack/ai-computer-use'
173import { playwrightComputer } from '@tanstack/ai-computer-playwright'
174
175declare const threadId: string
176
177const computerUse = createComputerUse({
178 driver: playwrightComputer({ startUrl: 'https://tanstack.com' }),
179 idleTimeout: 30 * 60_000,
180})
181
182// The conversation ended. Close the page of this thread.
183await computerUse.close(threadId)
184
185// The server stops. Close every page, then Chromium.
186await computerUse.close()
187```
188
189A thread that stays idle closes by itself. `idleTimeout` sets this time in ms:
190
191- The default is 10 minutes.
192- When the time ends, the page of the thread closes. The next action of that thread opens a new page at `startUrl`.
193- `false` keeps every thread open until you call `close`.
194
195### Keep the browser away from private addresses
196
197The 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.
198
199The `navigate` tool accepts only `http:` and `https:` URLs. It does not block private addresses.
200
201To keep the machine off your network, use a [sandbox driver](./drivers#linux-desktop-in-a-sandbox), for example E2B.
202
203### What the model can do
204
205The model clicks, double-clicks, drags, moves the mouse, scrolls, types text, and presses key combos. It can also wait.
206
207- A normal action returns a screenshot 500 ms later, so the page can settle. An OpenAI batch returns one screenshot after the last action.
208- `zoom` returns an image of one region. `cursor_position` returns the mouse position as text. Neither takes another screenshot.
209- `navigate` opens a URL, then returns a screenshot.
210
211To change the delay, set `screenshotDelay` in ms:
212
213```ts
214import { createComputerUse } from '@tanstack/ai-computer-use'
215import { playwrightComputer } from '@tanstack/ai-computer-playwright'
216
217const computerUse = createComputerUse({
218 driver: playwrightComputer({ startUrl: 'https://tanstack.com' }),
219 screenshotDelay: 1000,
220})
221```
222
223### Order and failures
224
225In one thread, actions run one at a time, in the order that the model calls them.
226
227- A driver factory, like `playwrightComputer()`, gives each thread its own machine. Those threads can run at the same time.
228- A driver that you pass in is one machine. Actions on that machine run one at a time, even from two threads.
229
230If one action fails, the other actions of that model turn do not run. The model gets this result for each of them:
231
232```text
233Not executed: an earlier computer action in this turn failed.
234```
235
236The next model turn starts clean. The model sees the error and can try again.
237
238### Limits
239
240- **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.
241- **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.
242
243### Model support
244
245- A model with no computer use is a type error at `chat()`.
246- The Anthropic adapter picks the newest computer tool version that the model supports. If the model supports no version, the adapter throws an error.
247- `openaiText` uses the Responses API, which has the computer tool. The Chat Completions adapters throw an error, because that API has no computer tool.