Workflow recipe
Schedule a recurring task for a coding agent
Schedule Claude Code tasks and Codex runs: write a self-contained prompt, cut connectors and network to the minimum, test with Run now, and know how to stop it.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOn this page
To schedule Claude Code tasks or Codex runs that you can trust while you are not watching, pick one job with a clear finish line, write a prompt that works with no one to ask, and give the task only the repository, network and connectors that job needs. Then create it with /schedule (Claude Code) or from a chat in the ChatGPT desktop app (Codex), run it once by hand, read the whole transcript, and practise pausing it before you let the timetable take over.
A scheduled agent run is a background agent with nobody assigning the work, so the two risks are a vague prompt and too much access. A prompt such as “keep the dependencies tidy” produces a different pull request every night. A task that kept every connector on your account can post to Slack or edit tickets as you, because scheduled runs act with your identity. This recipe uses a nightly dependency check as the example. The same steps fit a weekly issue sweep, a docs drift check or a Monday status digest.
Your notesAbout 10 min
1. Pick a job with a finish line
A good scheduled job has an input that changes on its own (new issues, new releases, merged pull requests), a fixed procedure, and a result someone reads. Write three lines before you open either tool:
- Trigger: when it should run, for example “weeknights at 02:10”.
- Done means: what exists after a good run, for example “one draft pull request per outdated direct dependency with a passing test run, or a single comment saying nothing needed changing”.
- Never: what it must not do, for example “merge, change CI files, bump a major version, or touch more than one dependency per pull request”.
If you cannot write “done means” in one sentence, the job needs a person each time. Keep it as a background agent task you hand over by hand instead.
Your editorAbout 15 min
2. Write a prompt that runs with nobody to ask
The prompt is stored once and replayed on every run, so it has to carry the whole job. State the goal, the procedure, the stop rules and where the report goes. Claude Code treats the saved prompt as the assignment, while text sent with an API trigger arrives as untrusted data, so put every instruction in the saved prompt.
Replace the bracketed parts. Keep the “stop if the default branch is already broken” rule: without it, the task spends the night trying to repair something unrelated to its job.
Claude Code on the web or the ChatGPT desktop appAbout 15 min
3. Cut the task’s access to what the job needs
Each tool decides what a run can reach in a different place. Narrow all of them before the first run.
A Claude Code scheduled task can reach three things: the repositories you select, its cloud environment, and the connectors you include.
- Repositories: select only the one the job works in. Each is cloned fresh on every run, and Claude pushes to branches that start with
claude/. - Environment: the Default environment has Limited network access with Allow package managers checked, which is enough for a dependency check. Leave it there. If the job needs an internal service, create a separate environment with only that domain under Allowed domains. Put API keys in network secrets, not environment variables, which anyone using the environment can see.
- Connectors: under Advanced settings > Connectors, every connector on your account is included by default, and a run can write through any of them. Remove all of them for this job. Add one back only if the report has to go somewhere other than GitHub.
Then check branch protection on GitHub. Anthropic’s docs say a rule your connected GitHub access can bypass does not block a run’s push, so make sure the default branch needs a review even from you.
A Codex scheduled task in the desktop app runs with your default sandbox settings and, where your organisation allows it, with approval_policy = "never", so nobody approves commands during the run.
- Where it runs: in a Git repository you choose between your local project and a new worktree. Choose the worktree, so the task’s changes stay away from your unfinished work.
- Sandbox: OpenAI’s advice is to start with the narrowest access that lets the task succeed. Workspace write is the usual choice for a dependency check: tool calls fail if they try to change files outside the workspace or reach the network, so check that your install command works within that before the first run. Do not give a scheduled task full access.
- Connected tools: a task can use the apps, skills and plugins on your account. Mention only the ones it needs in the prompt, and disconnect any you would not want it to write to.
Tasks that use local files need the computer on and the desktop app running when they are due.
Claude Code CLI or the ChatGPT desktop appAbout 10 min
4. Create the task and set the timetable
From a terminal in the repository, signed in with your claude.ai account:
/schedule weeknights at 02:10, run the nightly dependency checkClaude asks follow-up questions about the repository, the schedule and the prompt, then saves the task to your account. Paste the prompt from step 2 when it asks. On the web, the same task is New task at claude.ai/code/routines, where you fill in the name, prompt, trigger, repository, environment and model.
Two details about times. A run set for exactly the hour can start several minutes late, so pick a few minutes past. A custom cron expression with five fields is read in UTC, so 10 2 * * 1-5 is 02:10 UTC on weekdays. Runs more often than once an hour are rejected.
Open a Codex chat on the project in the ChatGPT desktop app and ask for the task in plain words:
Create a standalone scheduled task for this project that runs on weekdays at 02:10 in a new worktree, with this prompt: [PASTE THE PROMPT FROM STEP 2]A standalone task starts a new chat on every run and reports in Scheduled in the sidebar. A task attached to an existing chat returns to that chat with its context, which suits a short follow-up loop rather than a nightly job. If you keep the procedure in a skill, call it in the prompt with $skill-name so the prompt stays short. For an unusual timetable you can edit the task’s RFC 5545 recurrence rule, for example RRULE:FREQ=MONTHLY;BYMONTHDAY=1;BYHOUR=9;BYMINUTE=0 for nine o’clock on the first of each month.
The task's detail page or Scheduled viewAbout 15 min
5. Run it once now and read the whole transcript
Do not wait for the first scheduled night. In Claude Code, open the task and click Run now, then open the run from History. In Codex, OpenAI’s advice is to test the prompt by hand in a regular chat on the same project first, then read the first few scheduled runs in Scheduled before you trust it.
Read the transcript, not the status. Claude Code’s docs are explicit that Succeeded only means the session ended without an error: a blocked network request, a missing connector tool or work Claude could not finish appears in the transcript, not on the row. Check four things:
- the install and test commands ran, and the “stop if the default branch fails” rule would have worked;
- it touched only the files the prompt allows (open the diff of any pull request it made);
- it opened no more pull requests than your limit;
- the report says what it did in the format you asked for.
If any of these is wrong, change the prompt and run it again before the schedule fires. Most fixes are a missing stop rule or a test command that needs an environment variable.
Claude Code on the webAbout 5 min
6. Add an event trigger only if a timetable is the wrong fit
Some jobs should follow an event, not the clock: review each new pull request, or write release notes when a release is published. A Claude Code task can combine a schedule with a GitHub trigger on pull requests, releases or issues. The Claude GitHub App must be installed on the repository, and each event starts a fresh session.
Use filters so it fires only when it should, for example Is draft equals false to skip drafts, or Labels includes needs-backport. Every condition must match. GitHub events have hourly caps per task and per account, and events beyond them are dropped, so do not use a GitHub trigger for anything that must never be missed.
In Codex, event triggers for Gmail, Slack and GitHub are available on the web for eligible plans, not in the desktop app, CLI or IDE extension. A Codex task can use several event triggers but cannot mix them with a time-based schedule.
The task's detail page or Scheduled viewAbout 5 min
7. Practise stopping it before you need to
Know where the off switch is before the night the task opens twenty pull requests. Do each of these once now:
- Pause: the on/off switch at the top of the task’s detail page stops future runs and keeps the configuration. Switch it off and on again.
- Stop a run in progress: open the run from History and archive its session from the menu next to the session title. The run then shows as Cancelled.
- Cut API access: if you added an API trigger, Delete token removes it at once. Regenerate rotates it.
- Delete: the menu next to the task’s name has Delete.
- From the terminal:
/schedule listshows your tasks and/schedule updatechanges one.
On Team and Enterprise plans an Owner can turn scheduled tasks off for everyone under Organization settings > Capabilities > Remote sessions, which stops existing tasks too.
- Review and pause: Scheduled lists active, paused and completed tasks with their recent runs. Tasks are created and changed from a chat, so ask in a Codex chat on the project to pause or change the task, then check that Scheduled shows it as paused.
- Clean up worktrees: each worktree run leaves a worktree behind. Archive runs you no longer need, and do not pin runs unless you want to keep their worktrees.
- Organisation controls: admins can disallow
approval_policy = "never", limit the sandbox modes allowed, and control who may use event triggers with the Allow event-triggered scheduled tasks permission.
Write the task’s name, owner and off switch in your team’s runbook or AGENTS.md. Scheduled tasks belong to one person’s account, so a teammate cannot see or pause yours.
Check it worked
Verification checklist
0 of 7 done
When it goes wrong
If the run shows Succeeded but nothing happened, read the transcript. The usual causes are a network request blocked by the environment (a 403 or ENOTFOUND in the log) or a connector the prompt mentions that you removed. Add the domain to a dedicated environment, or put the connector back, then use Run now again.
If /schedule says “Unknown command” or asks you to log in, the CLI is using an API key or a cloud provider login instead of your claude.ai subscription. Unset ANTHROPIC_API_KEY in that shell, run /login with your claude.ai account, or create the task on the web instead.
If the task stops running after a few days, check the GitHub connection. Claude Code skips runs while the connection is missing or expired, and turns the task off after 72 hours; reconnect and switch it back on. In Codex, a task that worked locally stops when the computer sleeps or the project folder moves.
If the task keeps opening near-identical pull requests every night, the prompt has no memory of earlier runs. Add a rule to check for an open pull request with the same title first and skip that dependency if one exists.
Variations
For a job that needs files on your own machine, Claude Code also has desktop scheduled tasks and /loop, which run locally instead of in the cloud. To run an agent on your own CI runners and permissions instead of a vendor’s, use a scheduled GitHub Actions workflow with the agent in headless mode, as in headless coding agents in CI.
A weekly report is a gentler first task than a nightly code change: a Monday summary of merged pull requests and open incidents, written with the status report skill, needs read access only. Pair it with the weekly status update recipe for the format.
If the job edits code, add hooks to the repository as a second fence. The guardrail hooks recipe blocks force-pushes and edits to secrets whatever the prompt says.
Related background agent guides
The background coding agents comparison sets Claude Code’s cloud sessions beside Codex cloud, Copilot and Jules. For the team rules around delegated and scheduled work, follow the coding agents for teams path, and read the least privilege entry before you give any unattended task a connector.
Outcome and tools
- Outcome
- A recurring agent task, such as a nightly dependency check, that runs with only the access it needs, reports to one place, and that you have paused and resumed once.
- For
- Developers, Team leads
- Variants for
- Claude Code, Codex
- Tools
Related
- Copilot cloud agent vs Codex cloud vs Jules vs Cursor cloud agents
Background coding agents compared: Copilot cloud agent, Codex cloud, Jules and Cursor cloud agents on how tasks start, where they run, review and limits.
- Hand a GitHub issue to a background coding agent and review the PR
Write a brief it can finish, set up its environment, hand it to Copilot, Codex or Jules, then review and iterate once.
- Set up guardrail hooks for your coding agent
Block dangerous commands in Claude Code with PreToolUse hooks and deny rules, so the agent cannot force-push, wipe files or edit secrets. Test them first.
- Run coding agents headless in CI
Run claude -p and codex exec in GitHub Actions with pinned versions, narrow permissions, scoped secrets and separate jobs for writing back to the pull request.
- Least privilege
Granting an agent or tool only the permissions and data access its task requires, and nothing more, so a mistake does less damage.