playwright routine on one portal and the agent on the other four. You can manage routines in the Console, with the Deck CLI, or through the API.
When to write a routine
Start with the agent. It works out the steps from your prompt and works around changes on the source, so most tasks don’t need a routine. Consider a routine for a source when:- The agent keeps failing on agent mode. If runs on one source fail with
task_failedagain and again while other sources succeed, a routine can do the task there instead. - You need the same steps every time. For example, the data has to come from a specific page or from the portal’s own API, not wherever the agent finds it.
Routine types
A task can have one routine per source.
How a run uses a routine
When a task run starts, Deck looks up the routine for the run’s task and source:- If the run has a credential, the Deck agent logs in to the source. See Deck handles login.
- If the routine is
playwright, Deck calls your routine with the logged-in browser and the run’s input. - Whatever your routine returns becomes the run’s
output.
agent routine, the agent runs the task as usual.
Deck handles login
Your routine never logs in. Authentication is always handled by the Deck agent, which then hands the browser to your routine:- The Deck agent logs in with the run’s credential, including MFA. If the source asks for a code, the run pauses for an interaction, the same as any other run.
- Once login is complete, Deck passes the same browser to your routine, already logged in.
- If login fails, the run ends with the login error, such as
auth_invalidoraccount_locked, and your routine doesn’t run.
Two ways to get a routine
You can have Deck write the routine, or write it yourself. Both save a new version of the routine, and you can mix them: let Deck write the first version and edit it yourself, or the other way around.
See Let Deck build it, or start with Before you write to write it yourself.
Let Deck build it
A build logs in to the source, works through the task, and writes a Playwright routine from what it did. When the build completes, the routine gets a newplaywright version, and runs on that source use it right away. If the build fails, the routine doesn’t change.
Deck doesn’t check the routine’s output against the task’s output_schema before saving it. When the build completes, run the task on that source and check the run’s output. If it doesn’t match, the run fails with task_failed.
Start a build
In the Console, open the task’s Routines tab. For a source without a routine, click Create routine and select Deck builds routine. For a source that has one, select Build routine on anagent routine or Rebuild routine on a playwright routine. Pick a credential and enter an example input. The Instructions field starts with the task’s prompt. Edits there apply to this build only.
Through the API, create the routine as agent first if the source doesn’t have one yet:
The build only sees the parts of the portal available to the credential you provide. Pick a credential that covers all the parts of the portal the task needs to see.
The response is
202 with a routine_build in queued:
playwright routine, start another build on it. Each build writes a new version. Earlier versions stay in the version history.
With the Deck CLI, --wait starts a build and returns the finished one. If the build needs an interaction, the command returns as soon as the build is waiting.
Follow the build
Retrieve the build withGET /v2/routine-builds/{routine_build_id}, or subscribe to routine_build.* events. Builds use the same statuses as task runs:
If the source asks for an MFA code, the build pauses with an
interaction, the same as a task run. Submit the code with POST /v2/routine-builds/{routine_build_id}/interaction, using the same body as a task run interaction. To stop a build, call POST /v2/routine-builds/{routine_build_id}/cancel.
To see what a build did in the browser, read its logs with GET /v2/routine-builds/{routine_build_id}/browser-logs. They cover login and the build’s work on the task, and use the same shape and filters as task run browser logs. Retrieve the build with include=browser for its DOM and HAR files.
When you retrieve a build that’s running or interaction_required, live_view_url is a read-only view of its browser. It’s null otherwise, and in lists. Each retrieve returns a new URL that works for 30 minutes. The URL carries a token, so treat it like an API key. It’s always null for a sensitive task.
A build can run for up to 60 minutes, regardless of the task’s timeout_seconds. If it runs longer, it fails with timeout. If Deck can’t produce a routine, the build fails with routine_build_failed. Either way, the routine doesn’t change.
How builds affect the rest of your account
- Task runs. A build isn’t a task run. It doesn’t appear in
GET /v2/task-runsand doesn’t sendtask_run.*events. Runs of the task keep using the current routine version until a build saves a new one. - Credentials. A build doesn’t change a credential’s status or send
credential.*events. If login fails, the build fails with the login error, such asauth_invalid. With credential queueing on, a build holds the credential from when it starts until it ends, like a task run. - Edits. A routine can have one active build at a time. While a build is active, starting another build, updating the routine, or deleting it fails with
routine_build_in_progress. Cancel the build first. - Versions. Versions saved by a build have
routine_build_idset in the routine’s history.
Before you write: study an agent run
The quickest way to write a reliable routine is to see what the portal does when the agent completes the task. Run the task once with no routine, then read that run’s browser logs:navigation entries show the pages the agent visited in order. network entries show the XHR and fetch requests those pages made. Most portals load their data from a few JSON endpoints, and calling those endpoints from your routine is usually faster and more stable than reading the rendered page.
Write the routine
A routine is a Python module that definesexecute:
Your routine starts on whatever page the browser is showing after login, so navigate to where you need to be.
Return the output
The return value must match the task’soutput_schema exactly:
- Include every required field, and don’t set a required field to
null. - Don’t add fields the schema doesn’t define.
task_failed and output is null. print() doesn’t affect the output.
Fetch data from inside the page
Routines have no direct internet access. To call a portal’s API, run the request in the page, where it uses the page’s logged-in session:page.evaluate. Playwright’s request API (page.request and context.request) doesn’t work in routines.
Save files
To capture files, turn on storage for the task and save files to/downloads. After the routine succeeds, Deck uploads every file it wrote there to the run’s storage.
End with a specific outcome
Call adeck helper to end the run with a specific error code instead of a generic failure. Pass a message to replace the default one. The message is returned to you on the run.
Add
import deck at the top of the routine to use them. Messages are limited to 1,000 characters.
Helpers stop the routine immediately, and except Exception doesn’t catch them. Before calling a *_not_found helper on an empty list, confirm the page actually loaded, such as with wait_for_selector. A page that failed to load also looks empty.
Any other exception fails the run with task_failed. See When a routine fails.
Create the routine
A routine takes effect as soon as you save it. The next run of the task on that source uses it, so test it first with the local runner. To undo a change, restore an earlier version.In the Console
- Open the task and select the Routines tab.
- Click Create routine, select Build routine yourself, and pick the source.
- Write your routine in the editor. It starts with a template that defines
execute. - Click Save changes.
With the Deck CLI
Pass the task, the source, and your code inplaywright.script. This example builds the request body with jq, which reads routine.py as a string:
task_id, source_id, and name to deck routines update rtn_g7h8i9... --input - --yes.
Through the API
Create the routine with the task, the source, and the code inplaywright.script:
routine_already_exists. Update or delete the existing routine instead.
Routines can be up to 200,000 characters and run within the task’s timeout.
To see every routine a task uses, retrieve the task with include=routines:
Author locally with a coding agent
You can write and test a routine from your own machine with a coding agent such as Claude Code or Codex. Deck logs in to the source, then gives you access to the same browser your routine will get. The steps below use the Deck CLI, so your coding agent can run them itself. Each command maps to an API endpoint if you’d rather call it directly.1. Get a logged-in browser
Run the task once with the credential. The agent run from Before you write works, and so does a run that used your routine. When the run finishes, its session stays open and logged in for 10 minutes.session_id:
POST /v2/sessions/{session_id}/browser-access in the API. The command needs --yes because requesting access again replaces the earlier grant and drops its connection.
Connect Playwright to cdp_url to drive the browser. The URL carries a token, so treat it like an API key. Connect before expires_at, 30 minutes after the request. Once open, a connection stays up until you revoke access or the session ends. Only one connection can be open at a time.
The session stays open while your client is sending commands, and closes 10 minutes after its last command or after you disconnect. A session’s browser lasts up to an hour. Keep at least one tab open: closing the last tab ends the session. While Playwright is connected, you can’t start a task run in the session. Wait for any run in the session to finish before requesting access.
2. Test with a runner
Save this next to your routine asrun_routine.py. It calls your execute the same way Deck does and stands in for the deck module:
3. Brief your agent
Paste this into your coding agent, filled in with your task’s details:4. Ship it
When the routine works, end the session:routine.py as the routine with the Deck CLI.
Run the task on that source and check that the run’s output has what you expect. Retrieve the run with include=routines to confirm your routine ran.
Check which routine a run used
Retrieve a run withinclude=routines to see the prompt and the exact routine version it ran:
routines records the routine as it was when the run executed, so later edits don’t change it. An empty array means the source had no routine and the agent ran.
When a routine fails
When a routine raises an exception, runs past the task’s timeout, or returns output that doesn’t matchoutput_schema, the run fails with task_failed.
If login fails, the run ends with the login error, such as auth_invalid, and your routine doesn’t run. Errors from deck helpers keep their own codes.
Most routine failures come from a change on the portal, such as a moved element or a renamed endpoint. To fix one, reproduce it against a logged-in browser with the local runner, or start a build to have Deck write a new version.
Edit a routine
In the Console
Open the task’s Routines tab and select Edit routine on the source’s row. The routine’s version history is below the editor: select a version to compare it with the one before, or load it into the editor and save to restore it. To hand the source back to the Deck agent, select Remove routine. When a run that used a routine fails, its page in the Console links straight to that routine’s editor.Through the API
Update a routine with the fields you want to change:
Changing a routine to
agent removes its code. The old code stays in the version history.
Read browser logs
Deck records what happens in the browser during a task run: console output, XHR and fetch requests, and page navigations. Use them to learn how a portal works before you write a routine for it, or to see what went wrong on an agent run. Runs that use aplaywright routine record the login only, not your routine. Builds have browser logs too.
type (console, network, navigation, comma-separated) and page with limit and cursor, as with other list endpoints.
response_body_sample is often null, so use response_size and content_type to spot the requests that return data.
Credential values are masked, and request headers such as cookies are never recorded. Entries appear while the run is still in progress. An empty list means nothing was captured.
Browser files
Retrieve the run withinclude=browser to get two downloadable files:
domis the HTML of the last page the run was on, available once the run finishes or pauses for an interaction.haris every network request from the run as a HAR file, available once the run finishes.
url is an API path. Download it with your API key, like any other request. The HAR keeps request bodies as sent, so entries from login can include the user’s password.
Limits
Deck keeps the first 5,000 network, 10,000 console, and 1,000 navigation entries from each run. If a run goes past a limit, later entries of that type are dropped andlogs_truncated is true. The earliest entries, such as the login, are always kept.