Multi-Step Tasks

Break a big goal into subtasks the agent can tackle one at a time.

Agents shine on tasks that need several steps whose order or details are not known in advance. The key is decomposition: breaking the goal into smaller subtasks.

Patterns that help

  • Planning first — ask the model to outline the steps before acting.
  • One step at a time — act, observe the real result, then decide the next step (rather than blindly following an outdated plan).
  • Re-planning — if a step fails or reveals new information, revise the plan.

Keep each subtask small and verifiable so errors are caught early instead of compounding.

Example

Example · json
{
  "goal": "Publish a blog post about our new feature.",
  "plan": [
    "1. Draft the post from the feature spec",
    "2. Generate a title and summary",
    "3. Create the post via the CMS tool",
    "4. Verify it is live and report the URL"
  ]
}

When to use it

  • A travel planner agent breaks 'book a Paris trip' into: search flights, select best flight, search hotels, select hotel, confirm bookings — each as a separate step.
  • A code-migration agent decomposes 'migrate this module from Python 2 to 3' into: analyse imports, update syntax, fix string handling, run tests.
  • A report-generation agent splits 'write a market analysis' into: collect data, analyse trends, draft sections, review for consistency, format output.

More examples

Decompose a goal into subtasks

Uses the LLM to decompose a complex goal into ordered subtasks before the agent starts executing.

Example · python
from openai import OpenAI
import json

client = OpenAI()

r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role':'user','content':
        'Break this goal into 4 sequential subtasks, returned as a JSON array of strings.\n'
        'Goal: Write a blog post about climate change and publish it.'
    }],
    response_format={'type': 'json_object'}
)
plan = json.loads(r.choices[0].message.content)
for i, step in enumerate(plan.get('subtasks', []), 1):
    print(f'{i}. {step}')

Execute subtasks sequentially

Runs each subtask in order, passing previous results as context so later steps can build on earlier ones.

Example · python
from openai import OpenAI

client = OpenAI()

subtasks = [
    'Summarise the key facts about solar energy in 2 sentences.',
    'Write a catchy title for an article about solar energy.',
    'Write a one-paragraph intro for that article.',
]
results = {}
for i, task in enumerate(subtasks):
    context = '\n'.join(f'Step {j+1} result: {v}' for j, (k, v) in enumerate(results.items()))
    prompt = (f'{context}\n\nNow: {task}' if context else task)
    r = client.chat.completions.create(
        model='gpt-4o-mini',
        messages=[{'role':'user','content':prompt}]
    )
    results[f'step_{i}'] = r.choices[0].message.content
    print(f'Step {i+1} done: {results[f"step_{i}"][:60]}...')

Subtask with tool call per step

Runs a different tool call per subtask so each step in the multi-step plan has a concrete action.

Example · python
from openai import OpenAI
import json

client = OpenAI()

def web_search(query): return f'Top result for "{query}": example.com/article'
def save_note(note):   return f'Saved: {note[:40]}'
FNS = {'web_search': web_search, 'save_note': save_note}
tools = [
    {'type':'function','function':{'name':'web_search','description':'Search the web.','parameters':{'type':'object','properties':{'query':{'type':'string'}},'required':['query']}}},
    {'type':'function','function':{'name':'save_note','description':'Save a research note.','parameters':{'type':'object','properties':{'note':{'type':'string'}},'required':['note']}}},
]

goal_steps = ['Search for latest AI news', 'Save a note summarising the findings']
for step in goal_steps:
    msgs = [{'role':'user','content':step}]
    r = client.chat.completions.create(model='gpt-4o-mini', messages=msgs, tools=tools)
    msg = r.choices[0].message
    if msg.tool_calls:
        tc = msg.tool_calls[0]
        result = FNS[tc.function.name](**json.loads(tc.function.arguments))
        print(f'Step [{step[:30]}] -> Tool result: {result}')

Discussion

  • Be the first to comment on this lesson.