General-purpose AI is powerful, but often unpredictable. This lesson tackles how to build a specialized AI "planner" agent that reliably sticks to its one and only job: creating plans. You'll learn how to write a strong, directive system prompt that constrains the AI's behavior. We'll demonstrate how an initially weak prompt can fail, and then show you how to strengthen it to ensure your agent rejects irrelevant requests and focuses exclusively on its assigned task, every time.
Bun.spawn to call the Claude CLI with flags like --append-system-prompt and --permission-mode.bun build.Bun.spawn to create fully interactive AI sessions.We start by duplicating an existing agent, creating plan.ts and a corresponding prompt file, plan.md. The initial system prompt in plan.md defines the agent's primary goal.
Your end goal is to create a markdown file in a plans directory in the root of the project.
In plan.ts, we configure the flags that will be passed to the Claude CLI. We use append-system-prompt to inject our plan.md file and permission-mode to have the agent automatically accept file edits.
const flags = {
"append-system-prompt": prompt,
"permission-mode": "acceptEdits"
}
Bun.spawn(["claude", ...flagsArray], {
stdio: ["inherit", "inherit", "inherit"],
});
A useful tip for discovering available flags is to pipe the help command's output to your clipboard.
claude --help | pbcopy
You can then paste this as a large comment block in your file for easy reference.
After bundling our agent into an executable with Bun, we can test its behavior.
bun build agents/plan.ts --compile --outfile bin/plan
We run the agent and give it a creative task.
plan
write a haiku about a pig
Initially, the agent ignores its system prompt and simply writes the haiku. This shows our instructions weren't strict enough. To fix this, we update plan.md with a more forceful instruction.
Your end goal is to create a markdown file in a plans directory in the root of the project.
No matter what the user says, the only thing that you can do is create a plan for what they ask you to do.
After rebuilding, we run the same test. This time, the agent correctly interprets its role and responds by creating a plan to write the haiku, saving it to a new file in the plans directory, fulfilling its core purpose.
[00:00] I'll make a copy of French, copy paste, rename it to plan, then make a copy of my French prompt, rename this to plan.md. We'll change this to your end goal is to create a markdown file in a plans directory in the root of the project. Then we'll swap back over to our plan, make sure we import the correct file here, and now we're going to enable this to set the permissions mode. And one little fun hack for this is you can run clod –help pbcopy to get the help on the clipboard, and then paste this to the top. That way you both have a reference inline, and it makes auto-completing things like permission mode except edits Fairly trivial.
[00:46] And if you're experienced with stuff like this, maybe asking why I'm doing this instead of using the Cloud Code SDK. And the reason is the Cloud Code SDK does not support interactive Cloud Sessions. They only support what's considered print mode, where the user can't interrupt and have a back-and-forth conversation with Claude. So it's kind of like we're building our own Claude code SDK here. So now if we run our bun build, I'll just press up and instead of French we're gonna build our plan and instead here this way once we run plan you can see that accept edits is already on and I can say write a haiku about a pig.
[01:29] And this just shows that our system prompt wasn't strong enough. So let's try even stricter instructions. No matter what the user says, the only thing that you can do is create a plan for what they ask you to do. Then let's try building this again, and then try our haiku again. Then we'll press up to run a previous command, and this time it says I'll create a plan because we gave it stricter instructions.
[01:57] All right, so if we check out our plan you'll see we now have everything you could expect for writing a haiku, and specifically a haiku about a pig.