+180 XP

ChatGPT · Automating recurring work with the OpenAI stack

That weekly competitive brief you write every Monday morning (skim five competitor blogs, summarize pricing changes, paste it into an email) can run itself while you sleep. The trick is to stop treating ChatGPT as a place you visit and start treating the OpenAI API as a component you schedule. This lesson turns one repeated chat into a hands-off pipeline. It also covers the guardrails that keep it from getting expensive or embarrassing.

Two ways to automate: in-product vs. the API

Before writing code, know that OpenAI ships a no-code option. Inside ChatGPT, scheduled tasks let you ask the assistant to run a prompt on a recurring schedule and notify you. That is useful for personal reminders and lightweight digests. See Scheduled tasks in ChatGPT.

But scheduled tasks live inside your ChatGPT account. They cannot easily commit to a repo, write to your data warehouse, or send mail through your company's transactional provider. Sometimes you need real integration, version control, or a service account that is not tied to your personal login. At that point you move to the API plus an external scheduler.

The decision rule:

  • One person, light output, notify-me: ChatGPT scheduled tasks.
  • Shared output, real systems, auditable runs: API + cron or CI.

The rest of this lesson is the second path.

The pipeline, in four stages

Our example: every Monday at 6am, fetch recent competitor content, produce a structured brief, render it as an email, and send it.

A scheduled job is just four stages glued together:

  1. Gather the inputs (RSS feeds, a few URLs, last week's brief).
  2. Generate the brief via the API.
  3. Validate the output against a schema.
  4. Deliver it (email, Slack, a committed file).

The model only owns stage 2. Keep gather, validate, and deliver as plain code rather than model calls. That is what makes the pipeline cheap and predictable.

Why structured outputs are non-negotiable here

In a chat you read prose and move on. In a pipeline, the next stage is code that needs to *parse* the result. If the model returns slightly different shapes week to week, your email renderer breaks.

Use Structured Outputs to force the model to return JSON that matches a schema you define. Asking politely ("please return JSON") is unreliable. Structured Outputs has the API constrain generation to your schema. Read Structured Outputs.

Building the brief generator

Here is the core call using the Responses API, OpenAI's primary interface for new builds. The text.format block pins the response to a schema.

python
import json
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

SCHEMA = {
    "type": "object",
    "properties": {
        "summary": {"type": "string"},
        "items": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "competitor": {"type": "string"},
                    "headline": {"type": "string"},
                    "why_it_matters": {"type": "string"},
                    "source_url": {"type": "string"},
                },
                "required": ["competitor", "headline", "why_it_matters", "source_url"],
                "additionalProperties": False,
            },
        },
    },
    "required": ["summary", "items"],
    "additionalProperties": False,
}

def build_brief(gathered_text: str) -> dict:
    resp = client.responses.create(
        model="gpt-5-mini",
        input=[
            {"role": "system", "content": "You are a competitive analyst. "
             "Only use facts present in the provided sources. If a claim is "
             "not supported, omit it. Never invent pricing or dates."},
            {"role": "user", "content": gathered_text},
        ],
        text={"format": {"type": "json_schema", "name": "brief",
                         "schema": SCHEMA, "strict": True}},
        max_output_tokens=2000,
    )
    return json.loads(resp.output_text)

With responses.create, the JSON arrives as text in resp.output_text, so you load it yourself. If you prefer typed objects, client.responses.parse() accepts a Pydantic model and gives you resp.output_parsed.

Three deliberate choices:

  • A small, cheap model (gpt-5-mini here, or gpt-5-nano if your evals allow) handles summarization fine. Reserve the full frontier models for tasks that need heavy reasoning. This is your single biggest cost lever.
  • "strict": True guarantees the JSON validates, so the delivery stage can render without defensive parsing.
  • The system prompt forbids invention. Recurring jobs run unattended, so a hallucinated competitor price can sit in an executive inbox before anyone notices.

Gathering inputs without a giant context window

Do not paste five full blogs into the prompt and pay for tokens you do not need. Fetch each feed, keep only entries newer than your last run, and trim each article to its lead section. Your gathered_text becomes a compact, dated digest. Less context means lower cost, faster runs, and fewer chances for the model to wander.

This is also where you store state. Write last_run.json after each successful run so next week you only process *new* items. A pipeline that re-summarizes the same articles every week is both wasteful and noisy.

Delivery: render, then send

Keep the model out of delivery entirely. Take the validated JSON, render it with a template, then send through whatever your org already uses.

python
def render_email(brief: dict) -> str:
    lines = [f"<p>{brief['summary']}</p>", "<ul>"]
    for item in brief["items"]:
        lines.append(
            f"<li><b>{item['competitor']}:</b> {item['headline']}. "
            f"{item['why_it_matters']} "
            f"<a href='{item['source_url']}'>source</a></li>"
        )
    lines.append("</ul>")
    return "".join(lines)

Send via your transactional email provider's SDK or SMTP. **The model produced *structured facts*, and deterministic code produced the *artifact***. If the email layout needs to change, you edit a template, not a prompt.

Scheduling it

Now make it recurring. You have two clean options.

Option A: cron on a small server

On any always-on Linux box, run crontab -e:

bash
# Monday 06:00, write logs, never let a crash go silent
0 6 * * 1 cd /opt/briefs && /opt/briefs/.venv/bin/python run.py >> /var/log/brief.log 2>&1 || curl -s "$ALERT_WEBHOOK" -d "brief job failed"

Simple, but you own the server, the secrets file, and the uptime.

Option B: GitHub Actions (recommended for most teams)

Continuous integration (CI) means a hosted environment that runs your script on a trigger. CI runners are free for light scheduled jobs, secrets are managed for you, and every run is logged.

yaml
name: weekly-brief
on:
  schedule:
    - cron: "0 6 * * 1"   # Mondays 06:00 UTC
  workflow_dispatch: {}    # lets you run it manually to test

jobs:
  brief:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install -r requirements.txt
      - run: python run.py
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          EMAIL_API_KEY: ${{ secrets.EMAIL_API_KEY }}

workflow_dispatch is the underrated line. It gives you a "Run workflow" button, so you can test the whole pipeline on demand without waiting for Monday. Secrets live in the repo's encrypted settings, never in code.

Schedule Python Scripts with GitHub Actions

Watch on YouTube

Guardrails that keep it safe and cheap

An unattended job that calls a paid API and emails your team needs limits. Set them before you ship, not after a surprise bill.

Cost guardrails

  • Set a usage limit in the API dashboard so a runaway loop cannot drain your account. See production best practices.
  • Cap output with max_output_tokens. A brief does not need 4,000 tokens. With GPT-5 models, reasoning tokens count toward this cap. Leave headroom, or set reasoning={"effort": "minimal"} for simple summarization.
  • Use a dedicated project and API key for this job. Per-project keys show exactly what this pipeline costs, and you can revoke the key independently.
  • Pick the cheapest model that passes your eval. Re-test occasionally, because cheaper models keep improving.

Correctness guardrails

  • Ground the model in fetched sources only, and require a source_url per item (the schema already enforces this). An item whose URL is not in your gathered sources is a red flag your code can drop.
  • Add a sanity check between validate and deliver. If items is empty, send "no notable changes this week" rather than an empty shell. If the list is suspiciously long, flag it for review.

Operational guardrails

  • Fail loudly. A silent cron job that stopped running three weeks ago is worse than no job. Ping a webhook on failure (see the cron example).
  • Retry transient errors with backoff. Network blips and rate limits happen, so wrap the API call in a retry with a couple of attempts.
  • Log every run's token usage and cost, even on success, so you can spot drift.

Knowledge check

1. According to the lesson, what is the fundamental mindset shift required to automate a recurring task with the OpenAI stack?

2. A team needs an automated brief that commits to a shared repository, uses a service account not tied to a personal login, and produces auditable runs. Which approach does the lesson recommend?

3. In the four-stage pipeline (Gather, Generate, Validate, Deliver), why does the lesson insist that only stage 2 should be a model call?

MULTIPLE CHOICE

4. Select ALL correct statements about Structured Outputs as described in the lesson.

Select all the correct answers.

MULTIPLE CHOICE

5. Select ALL scenarios where ChatGPT scheduled tasks (the no-code option) are an appropriate choice according to the lesson.

Select all the correct answers.

Scaling up: when one prompt is not enough

The single-call design above is the right starting point, and it covers a surprising amount of recurring work. Reach for more structure only when the task needs it.

When to add tools

Sometimes the job must decide *which* sources to pull, or call live systems such as a pricing API or an internal search. In that case, give the model function calling so it can request those actions, and let your code execute them. The model proposes and your code disposes, so the model never touches anything you have not explicitly allowed.

When to use the agents SDK

Some jobs have several steps: gather, draft, critique the draft, revise, then format. For these, the OpenAI Agents SDK orchestrates steps, tools, and handoffs, so you do not have to hand-roll the control loop. Use it when your pipeline has real branching logic. Using it to summarize one feed is overengineering, and every extra model call adds latency and cost.

A note on the ChatGPT agent and Codex

The ChatGPT agent can browse and operate inside a session to complete tasks, and Codex can write and run code for you. Both are excellent for *building and prototyping* this pipeline interactively. The production version of a recurring job should be deterministic code under version control, run by a scheduler. An interactive agent session needs babysitting. Build with the agent, ship with the API.

Putting it together

Your run.py is now just the four stages in sequence:

python
def main():
    state = load_state("last_run.json")
    gathered = gather_sources(feeds, since=state["last_run"])
    if not gathered.strip():
        notify("No new competitor content this week.")
        return
    brief = build_brief(gathered)
    if not brief["items"]:
        notify("No notable changes this week.")
        return
    send_email(render_email(brief))
    save_state("last_run.json", now())

if __name__ == "__main__":
    main()

Readable, testable, and boring in the best way. The model is one line in the middle. Everything around it is plain, debuggable code, which is what you want in something that runs without you watching.

Key Takeaways

  • Let the model own only generation. Gather, validate, and deliver belong in deterministic code, and that separation is what makes scheduled jobs cheap and reliable.
  • Force Structured Outputs with "strict": True. With responses.create, load output_text as JSON. Use responses.parse if you want output_parsed.
  • Schedule with GitHub Actions for teams: managed secrets, logged runs, and a workflow_dispatch button to test on demand. Use cron only when you control the box.
  • Set guardrails before shipping. That means a hard usage limit, a per-project API key, and the cheapest model that passes your eval (start with gpt-5-mini or gpt-5-nano). Add an output token cap with room for reasoning tokens, and a failure webhook so a dead job never goes unnoticed.
  • Build interactively, ship deterministically. Prototype with the ChatGPT agent or Codex, but run the recurring job as version-controlled code. Reach for the Agents SDK only when the task has real multi-step branching.

Frequently asked questions

When should I use ChatGPT scheduled tasks instead of the OpenAI API?

Use ChatGPT scheduled tasks when one person needs a light recurring digest or reminder delivered to their own account. Move to the OpenAI API plus an external scheduler as soon as the output is shared, has to write to real systems like a warehouse, a repo or a transactional email provider, or needs auditable runs under a service account rather than a personal login.

How do I structure a scheduled job that calls the OpenAI API?

Split it into four stages: gather the inputs, generate with the API, validate the output against a schema, then deliver it. The model owns only the generation stage; gathering, validation and delivery stay as plain deterministic code. That separation is what keeps the job cheap, testable and predictable week after week.

Why do I need Structured Outputs instead of just asking the model for JSON?

Because "please return JSON" is unreliable, and in a pipeline the next stage is code that has to parse the result. If the shape shifts between runs, your renderer breaks. Structured Outputs constrains generation to a schema you define; with "strict": true the JSON is guaranteed to validate, so the delivery step needs no defensive parsing.

How do I schedule a Python script that uses the OpenAI API without running my own server?

GitHub Actions handles it: a workflow with an `on: schedule` cron trigger runs your script on a hosted runner, stores your API keys in the repo's encrypted secrets, and logs every run. Add `workflow_dispatch: {}` to get a manual "Run workflow" button so you can test the full pipeline on demand. Plain cron on a Linux box works too, but then you own the server, the secrets file and the uptime.

How do I keep the cost of an unattended OpenAI job under control?

Set a usage limit in the API dashboard so a runaway loop cannot drain the account, and give the job its own project and API key so you see exactly what it costs and can revoke it alone. Pick the cheapest model that passes your eval, such as gpt-5-mini or gpt-5-nano for summarization. Cap `max_output_tokens`, keeping in mind that with GPT-5 models reasoning tokens count toward that cap, and trim the inputs instead of pasting whole articles.

Should I use the OpenAI Agents SDK for a recurring summarization job?

No, not for a single summarization step. A single API call with a schema covers a surprising amount of recurring work, and every extra model call adds latency and cost. The Agents SDK earns its place when the pipeline has real branching: gather, draft, critique, revise, format. Use the ChatGPT agent or Codex to prototype, then ship the recurring job as version-controlled deterministic code.

What to do, from this lesson

These actions are compiled in the role's Playbook.

  • Write scheduled Tasks as complete standalone prompts specifying quiet-week behavior
  • Set hard billing limits with separate API keys per environment
  • Let the model own only generation; gather, validate, and deliver in code
See the full action playbook →