Create Your First AI Planning Agent in 10 Minutes (n8n Tutorial)
A step-by-step n8n tutorial for an AI planning agent. A Webhook node receives a goal, a Code node builds the OpenAI request with the planning prompt, an HTTP Request node calls OpenAI in JSON mode, a second Code node validates the plan and falls back safely on bad JSON or a failed call, and a Respond to Webhook node sends the plan back.

This tutorial is for developers and makers who want an AI step that turns a vague goal into a plan they can act on. You will build a small n8n workflow: it receives a goal over a webhook, asks an OpenAI model for a task list in JSON, checks the result, and sends the plan back. You get the exact prompt, the node settings, and two Code nodes that were tested on their own.
Key takeaways
- The prompt does the planning. Give it a fixed JSON shape, a task limit and an example, and turn on JSON mode in the API call.
- Never trust the model's output blindly. A Code node parses and checks the plan, and returns a safe fallback when the JSON is bad or the API call fails.
- Build the request body in a Code node, not by pasting text into a JSON field.
JSON.stringifyhandles the quotes for you.
Why build an AI planning agent?
Developers and creators juggle projects that need to be broken down step by step. Doing that by hand takes time, and the result changes from one day to the next. A planning agent does this first pass for you: it turns a high-level goal into ordered tasks with dependencies.
An AI planning agent is a program that takes a goal and uses a language model to break it into specific, ordered tasks. The one in this tutorial is closer to a workflow than an autonomous agent: the steps are fixed, and only the plan comes from the model. That is a feature. If you're unsure which you need, read LLMs vs AI agents vs workflows.
This isn't another chatbot. The agent uses a structured prompt to get consistent, usable output. Give it "Build a landing page" and it might return ordered tasks such as "1. Design wireframe, 2. Write copy, 3. Build the HTML and CSS, 4. Add analytics."
Prerequisites and setup
Before you build the workflow, make sure you have:
- An n8n instance (n8n Cloud or self-hosted)
- An OpenAI API key, saved in n8n as an OpenAI credential
- Basic familiarity with adding and connecting n8n nodes
n8n's OpenAI credential needs only the API key; the organization ID is needed only if you belong to more than one organization. If you are new to n8n, the n8n guide with a Telegram translation workflow walks through the editor first.
Building the n8n workflow
The workflow has five nodes in a straight line:
- Webhook: receives the planning request with the goal.
- Build request (Code): puts the planning prompt and the goal into an OpenAI request body.
- HTTP Request: sends that body to the OpenAI API.
- Validate plan (Code): checks the reply and falls back safely when something is wrong.
- Respond to Webhook: sends the plan back to the caller.
Step 1: The Webhook node
Add a Webhook node and set:
- HTTP Method: POST
- Path:
plan - Respond: Using 'Respond to Webhook' Node
n8n's docs on expressions use a webhook example in which the request body sits under body, as in {{$json.body.city}}. So the goal arrives as body.goal. Check this in a test run, because the field names come from whatever the caller sends.
Step 2: The planning prompt, in a Build request node
The intelligence of the agent lives in its prompt. It asks for a fixed JSON shape, caps the plan at eight tasks, and shows one example. The prompt contains double quotes, so pasting it by hand into a JSON body would break the JSON. Instead, add a Code node named Build request, in Run Once for All Items mode with JavaScript, and let it build the whole request body:
const SYSTEM_PROMPT = `You are a professional project planning assistant. Break the user's goal into actionable, sequential steps.
Rules:
1. Always reply with valid JSON in this shape:
{"goal": "original goal", "tasks": [{"id": 1, "description": "task", "estimated_time": 30, "dependencies": []}]}
2. Each task must be a specific, executable action.
3. estimated_time is a whole number of minutes.
4. dependencies lists the ids of tasks that must finish first.
5. Return at most 8 tasks.
Example input: Create a newsletter system
Example output: {"goal": "Create a newsletter system", "tasks": [{"id": 1, "description": "Design the email template in HTML", "estimated_time": 45, "dependencies": []}, {"id": 2, "description": "Set up the mailing list database schema", "estimated_time": 30, "dependencies": []}]}`;
const goal = $input.first().json.body.goal;
return [{
json: {
model: 'gpt-6-luna',
response_format: { type: 'json_object' },
messages: [
{ role: 'system', content: SYSTEM_PROMPT },
{ role: 'user', content: String(goal ?? '') },
],
},
}];
Checked against n8n's docs and OpenAI's API reference, 2026-10-30.
The fixed shape keeps the output parseable, the task cap limits scope creep, and the dependency list gives you an order to work in. The time is a number, not text, so you can add the minutes up later. The prompt sits in a JavaScript template literal (between backticks), so its double quotes need no escaping.
Three notes on the request:
- The model. On the date above, OpenAI's models page calls
gpt-6-lunaits most efficient model for focused, high-volume tasks, which suits a short planning call. Model ids change often, so check that page before you pick one. - JSON mode. OpenAI's API reference says
json_objectmode makes sure the reply is valid JSON, and warns that the model won't produce JSON unless a message asks for it. The prompt does. The same reference recommends the stricterjson_schemaformat for models that support it. - The return value. n8n's data structure docs say every item is an object with a
jsonkey, so the code returns[{ json: ... }].
Step 3: The HTTP Request node
Add an HTTP Request node after Build request and set:
- Method: POST
- URL:
https://api.openai.com/v1/chat/completions - Authentication: Predefined Credential Type, with Credential Type set to OpenAI and your OpenAI credential selected
- Send Body: on, with Body Content Type JSON and Specify Body set to Using JSON
- JSON: switch the field to Expression mode, then enter
{{ JSON.stringify($json) }}
n8n's docs on expressions say to switch a field to expression mode before you type an expression into it. In Fixed mode, n8n would send the text {{ JSON.stringify($json) }} as it is. The expression turns the Build request output into the JSON text of the body, with every quote escaped.
n8n's HTTP Request docs recommend a predefined credential type when one exists. It keeps your key out of the node, unlike a hand-written Authorization header. The older version of this post pasted the key into a header, pasted the prompt into the body by hand and used an old model id; all three are fixed here.
Then open the node's Settings and set On Error to Continue (using error output). n8n's node settings docs say this option passes the error information on to the next node. Connect both the success output and the error output to the next node, so a failed API call still reaches the fallback.
Step 4: The Validate plan node
Add a second Code node named Validate plan, in Run Once for All Items mode with JavaScript. It parses the model's reply and checks the structure. If anything is wrong, including an error item from the HTTP Request node, it returns a fallback plan and the reason:
const goal = $('Webhook').first().json.body.goal;
try {
const content = $input.first().json.choices[0].message.content;
const plan = JSON.parse(content);
// Validate the structure before anything downstream uses it
if (!Array.isArray(plan.tasks) || plan.tasks.length === 0) {
throw new Error('Invalid task structure');
}
return [{ json: plan }];
} catch (error) {
// Fallback: return one manual task and the reason, so the caller still gets valid JSON
return [{
json: {
goal,
tasks: [
{ id: 1, description: 'Break down the goal into subtasks manually', estimated_time: 15, dependencies: [] },
],
error: error.message,
},
}];
}
The reply text sits in choices[0].message.content, as OpenAI's API reference describes. An error item has no choices, so reading it throws and the fallback runs. $('Webhook').first() reads the webhook's output, using the syntax from n8n's docs on the output of other nodes. That matters because $input here is the HTTP Request output, which no longer holds the goal. The node name in $('Webhook') must match the name on your canvas.
Both Code node bodies were tested outside n8n with Node.js 22, with $ and $input stubbed. The Build request output, passed through JSON.stringify as the HTTP Request expression does, parsed back as valid JSON, including for a goal with double quotes in it. Validate plan returned the plan for valid JSON, and the fallback for invalid JSON, a missing tasks array, an empty reply and an error item.
Step 5: The Respond to Webhook node
Add a Respond to Webhook node at the end and set Respond With to First Incoming Item. As long as the workflow reaches this node, the caller gets valid JSON: the plan, or the fallback with its error field.
Testing and iteration
Click Execute workflow in the editor, then call the webhook's Test URL. On a local install it looks like this:
curl -X POST "http://localhost:5678/webhook-test/plan" \
-H "Content-Type: application/json" \
-d '{"goal": "Plan a product launch"}'
Try varied goals:
- "Plan a product launch"
- "Automate social media posting"
- "Build a customer feedback system"
Judge each plan on three things:
- Actionability: can someone start the task right away?
- Sequencing: do the dependencies make a sensible order?
- Time realism: are the estimates reasonable?
If tasks are too vague, add specific requirements to the prompt. If the order is off, make the dependency rule stricter. When the plans look right, publish the workflow. n8n's Webhook docs say the production URL only works once the workflow is published, and its runs show up in the Executions tab instead of the editor.
Production enhancements
Once the basic agent works, consider these upgrades:
- Add memory: store past plans in a database so the agent can reuse plans for similar goals.
- Chain more steps: pass the plan to a research step or a review step before anyone acts on it.
- Human in the loop: add an approval step before a plan creates real work.
- Integrations: create tickets in Notion, Trello or Jira from each task through their APIs.
The agent is most useful when its plans land where you already work, as tickets, calendar events or docs. For an n8n workflow that writes into Notion, see the AI inbox triage tutorial.
Common pitfalls and solutions
Problem: the model returns text that isn't JSON. Solution: keep JSON mode on and the word JSON in the prompt. The Validate plan node's fallback catches whatever still gets through.
Problem: tasks are too generic. Solution: add examples of specific and vague tasks to the prompt, for example: "Specific: 'Install PostgreSQL', Vague: 'Set up database'."
Problem: time estimates are unrealistic. Solution: give the prompt anchor estimates, such as "Writing 500 words = 30 minutes, a simple API endpoint = 60 minutes." Treat these anchors as your own rules of thumb and tune them to your team.
Problem: the goal comes back empty in the plan.
Solution: check where your caller puts the goal. If it isn't under body.goal, update the goal line in both Code nodes.
Next steps
You now have a working planning agent that breaks goals into tasks. From here you can:
- Connect a calendar API to schedule the tasks
- Add priority scores based on deadlines
- Make separate planners for content, development or marketing
- Keep a library of plan templates for projects you repeat
The real value comes when you stop doing the first breakdown by hand and spend that time on the work itself.
Frequently asked questions
Do I need the OpenAI node, or is the HTTP Request node enough?
The HTTP Request node is enough and shows you the exact API call, which helps you learn. n8n's docs recommend a predefined credential type in the HTTP Request node, so your key stays in n8n's credential store either way. Use the OpenAI node if you prefer a form over writing the JSON body.
Why does Validate plan read the goal from the Webhook node?
Each node receives the output of the node before it. In Validate plan, that is the OpenAI response, which doesn't contain the goal. $('Webhook').first() reads the webhook's output directly, so the fallback can still return the original goal.
What happens if the model returns invalid JSON?
JSON.parse throws, the catch block runs, and Validate plan returns a one-task fallback plan with an error field. The same happens when the API call fails, because the HTTP Request node's error output also leads there. The caller gets valid JSON and can show the error or retry.
Which model should I use for planning?
A small, efficient model is usually enough for a short planning prompt. Move to a stronger model if the plans are too shallow. Check OpenAI's models page for current ids, because they change often.
Is this a real agent?
It's a fixed workflow with one AI step, and for planning that's usually what you want. It is predictable and easy to test. Add tool calls and loops only when the task needs the model to choose its own next step.
Sources
Verified against the sources below on October 30, 2026. Products and docs change often: check the linked sources if something looks different.
- n8n Docs: Webhook node
- n8n Docs: Respond to Webhook node
- n8n Docs: HTTP Request node
- n8n Docs: Custom API operations (predefined credentials)
- n8n Docs: OpenAI credentials
- n8n Docs: Expressions for data transformation
- n8n Docs: Node settings
- n8n Docs: Code node
- n8n Docs: Data structure
- n8n Docs: Output of other nodes
- OpenAI API reference: Create chat completion
- OpenAI API: Structured Outputs
- OpenAI API: Models
AI Automation Engineer & Salesforce Developer
Salesforce developer since June 2018. I build AI agents, n8n workflows and Salesforce integrations, and write up what holds up in real use.



Comments
Loading comments...