Lesson
Build an Autonomous Agent with Pre-Approved Tools
Build autonomous AI agents with pre-approved tools. Use allowedTools and system prompts to enable research, planning, and execution without oversight.
- Access
- Included
- Transcript
- Needs source
Why manually approve every action when your agent can work on its own? This lesson shows you how to build a truly autonomous agent by giving it pre-approved "tools" like web search and file editing capabilities. You will learn a powerful technique: using Claude's own documentation as context to teach the agent how to configure itself with the allowedTools flag. By combining this with a system prompt that requires it to perform research, you'll create an agent that can take a high-level goal, research the topic, and generate a detailed plan—all without any manual intervention.
The Core Technique: Documentation as Context
The foundation of this workflow is providing Claude with the knowledge it needs to configure itself. By copying the official Claude Code settings documentation and pasting it into a new session, you give the AI the complete context required to understand and implement its own configuration flags.
Configuring the Agent for Autonomy
Once the context is provided, you can instruct Claude to modify its own runner script. The goal is to add the allowedTools flag, which tells the agent which actions it can perform without asking for permission.
const flags: ClaudeCodeFlags = {
"append-system-prompt": prompt,
"permission-mode": "acceptEdits",
"allowedTools": "WebSearch Write Edit MultiEdit NotebookEdit",
};
Directing the Agent with System Prompts
With tools pre-approved, you can use a system prompt to enforce their use. By adding a simple instruction to the agent's core prompt, you can ensure it always performs research before acting.
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.
Always include at least one web search to research the subject before creating the plan.
This combination results in an agent that can receive a high-level task, research it, and generate a detailed plan, all in a single, non-interactive execution.
Bonus: Generating TypeScript Types from Documentation
The same documentation context can be used for improving the developer experience. You can ask Claude to generate precise TypeScript types for all available flags, giving you type safety and autocompletion in your agent's configuration.
interface ClaudeCodeFlags {
"debug"?: boolean;
"verbose"?: boolean;
"print"?: boolean;
"output-format"?: OutputFormat;
"input-format"?: InputFormat;
"allowedTools"?: string; // Space or comma-separated list of Tool names or patterns
"disallowedTools"?: string; // Space or comma-separated list of Tool names or patterns
// ... and many more
}
Running the Agent as a CLI Tool
Finally, the script can be adjusted to accept a user prompt directly from the command line, turning your agent into a reusable and powerful CLI tool.
const args = process.argv.slice(2);
Bun.spawn(["claude", ...args, ...flagsArray], {
stdio: ["inherit", "inherit", "inherit"],
});
This allows you to execute complex, multi-step tasks with a single command.
Spoken Prompts
Please implement it so that I can use the allowed tools in my flags. And the tools I want to allow for this are the web search and anything related to writing or editing files.
Always include at least one web search to research the subject before creating the plan.
I want to build a CLI to interact with Zoom.
Based on the documentation for settings and the available options in the Claude Code CLI, please write out proper types for our flags to help us get proper typing and autocomplete in TypeScript.
Create a playwright script which scrapes hacker news.
Terminal Commands
# Start a Claude session
claude
# Build the agent script into an executable
bun build agents/plan.ts --compile --outfile bin/plan
# Run the agent with a prompt passed as an argument
plan "I want to build a CLI to interact with Zoom."
# Run the agent with a different prompt
plan "Create a playwright script which scrapes hacker news."