Gemini Computer Use
Run Gemini computer use on a browser that SpecStream hosts. One call opens a session, Playwright connects to its connect_url, and Gemini drives the page: it reads a screenshot, answers with an action, and your loop carries it out. The browser costs $0.10 an hour, charged by the second, so a ten minute task costs under 2 cents of browser time. The script below is the whole loop in one Python file.
What you need
- A Gemini API key from Google AI Studio (opens in a new tab), set as
GEMINI_API_KEY, and Python withgoogle-genai2.7.0 or higher, Playwright and requests. - A SpecStream key. Get your key on the Search API screen of your SpecStream workspace. New accounts start with $10 of credit, no card.
- 1
1. Install the packages and set both keys
Google's guide also runs
playwright install chromium. You can leave that out here: the browser runs at SpecStream, and Playwright on your machine only connects to it.Terminal - 2
2. Open a browser session
POST /v1/browser/sessionsopens the browser. Every field is optional.timeout_ssets the most seconds the session runs, from 60 to 3,600, andviewportsets the screen Gemini sees. Google's examples use 1440 by 900, so the script asks for the same.The reply holds the session under
data: itsid, aconnect_urlthat speaks the Chrome DevTools Protocol, and alive_urlthat shows the browser's screen while the agent works. The token inconnect_urlbelongs to this session alone, so you can hand it to a sandbox without your API key.Terminal - 3
3. Save the agent loop
Save this as
gemini_agent.py. It opens the session, connects Playwright withconnect_over_cdp, and sends Gemini the goal with a first screenshot throughclient.interactions.createand thecomputer_usetool in thebrowserenvironment.Each turn, the script reads the
function_callsteps, carries out each action, and sends back onefunction_resultper call with the page's URL and a new screenshot.previous_interaction_idcontinues the same task. The loop ends when Gemini answers with text and no action, or afterMAX_TURNSturns.Gemini gives coordinates on a 1000 by 1000 grid.
pxscales each one to the viewport before the click.acthandles every action Google lists for the browser environment, includingscroll,drag_and_drop,press_keyandhotkey.The
finallyblock releases the session, so the charge stops that second even when the script fails, and prints what the session cost.gemini_agent.py - 4
4. Run it
Change
GOALandSTARTto your task, then run the script. It prints thelive_urlfirst: open it to watch the browser. Each step prints the action and Gemini'sintent, the reason it gives for that action. The last lines are Gemini's answer and the session's cost.Terminal
When Gemini asks before a step
Gemini can attach a safety_decision to an action, for example before a payment, a sent message or a cookie consent. When the decision is require_confirmation, Google's rule is that the end user is asked. The script prints Gemini's explanation and waits for y. After a yes it carries out the step and sets safety_acknowledgement in the result, as Google's guide shows. After a no it stops, and the session is released.
In a product, replace the input call in allowed with your own approval screen. Keep the stop: an agent that skips the question can buy, send or accept something for your user.
Keep the agent on the sites you chose
allowed_domainson the session lists the hosts the browser may open, such asexample.comor*.example.com. A page outside them is refused, whatever the model asks for.timeout_sends the session after that many seconds, connected or not, so a loop that never finishes cannot run all day. The most is 3,600.MAX_TURNSends the loop on your side. Google's sample stops at 5 turns. Most real tasks need more, so the script uses 20.block_mediaset to true skips images, video, audio and fonts. Leave it off for computer use: Gemini reads the screenshot, and a page without images is harder to read.- To add Google's own check for hidden instructions in a page, add
"enable_prompt_injection_detection": Trueto the tool inTOOLS.
Choose the model
The script uses gemini-4-hydrogen, the model in Google's examples. Google's guide also lists gemini-4-carbon, gemini-4-argon, gemini-3.8-flash, gemini-3.5-flash-lite and gemini-3-flash-preview for computer use. Change MODEL and nothing else. Code written for the older gemini-2.5-computer-use-preview-10-2025 used generate_content and other action names, so it does not run on this loop as it is.
If something doesn't work
| What you see | What to do |
|---|---|
| Clicks land in the wrong place | The coordinates were not scaled, or the viewport differs from WIDTH and HEIGHT. Keep px on every coordinate and the same two numbers in the session's viewport. |
401 when the session opens | The SpecStream key is unknown, expired or revoked. Create a new key in your workspace and set SPECSTREAM_API_KEY again. |
402 when the session opens | The balance does not hold the first minute of the session. Add balance in your SpecStream workspace. |
422 when the session opens | A field is wrong, and the reply names it. timeout_s goes from 60 to 3,600. |
| Playwright cannot connect | The session has ended, so its connect_url no longer works. A session ends at its timeout_s, and at once when the client disconnects. Open a new session. |
A step returns an error that names a blocked page | The host is outside allowed_domains, is a private address, or the site opted out. Gemini reads the error in the result and can choose another step. |
No handler for ... in a result | Gemini called an action the script does not know, such as a custom function you added. Add a branch for it in act. |
| The loop ends without an answer | The task needed more than MAX_TURNS turns. Raise it, and raise timeout_s with it. |
Cost
Browser time costs $0.10 an hour, charged by the second. Driven over connect_url with Playwright, as on this page, steps are not charged: a ten minute session costs $0.017 and the longest session, one hour, costs $0.10. The session's charged_usd shows the amount, and the script prints it. Sending one step at a time to the session's /actions route costs $1 per 1,000 steps on top of the time.
Google bills the Gemini tokens, and each turn sends one screenshot. The rates are on pricing.
When the task only needs to read
Computer use is for pages that need clicks: a form, a filter, a flow behind a button. For the text of a page, the Extract API is $0.80 per 1,000 pages, with a real browser where the page needs one. For an answer from the web, the Search API is $5 per 1,000 searches of up to 10 results. Both run on the same key, and the REST API reference lists every call. See also headless browsers for AI agents and the Browser Use guide.
Remove
Delete gemini_agent.py and revoke the key in your SpecStream workspace. A session that is still open ends at its timeout_s. To end it now, call POST /v1/browser/sessions/{id}/release. Questions: [email protected].
More
SpecStream for Gemini Computer Use covers what it does in Gemini Computer Use on one page. Every other tool has its guide in Connect your tools. You see the most a request can cost before it starts, and the matches are paid from your balance. Billing has the details. For help, email [email protected].