Agents and Subagents: Roles, Boundaries, and Delegation
A practical guide to models, instructions, permissions, tools, contexts, agents, and subagents in modern AI coding frameworks.

AI agents are rapidly becoming a core part of modern software development. Tools like Opencode, Claude Code, and Codex allow developers to delegate coding, research, testing, and documentation tasks to model-driven workers commonly known as agents. However, many developers don’t fully understand the difference between an agent runtime, an agent, and a subagent.
In this article, we’ll break down how agents work, how to create your own custom agents and subagents, and how to use them effectively in Opencode and other agent frameworks.
Recently, while working on a project in Opencode, I noticed there were only two modes available — “build” and “plan”. What I really wanted was something in between, an “ask” mode that could let me prompt the AI for explanations on specific concepts and implementations without the risk of it editing files or performing unintended actions. After digging into how to set it up, I discovered that the build and plan modes in Opencode are actually referred to as agents, not Opencode itself.
I realized that, like many developers, I mistook tools like Opencode and Claude Code for agents, but in reality, they are simply agent runtimes or frameworks that provide the infrastructure for model access, tool execution, task planning, and agent or subagent delegation.
What are agents?
Since Opencode and Claude code aren’t agents themselves, what then are agents? Agents are systems that can pursue a goal by reasoning about tasks, using tools, gathering feedback, and taking actions toward an objective. More plainly, an agent is a configuration for a model-driven worker that uses a language model with instructions or system prompts within a defined set of tools, permissions, context, and execution constraints to pursue and achieve a goal. It defines the role, limits, and working style of the model.
A model-driven worker is a software process, similar to a background or web server worker, but its decision-making is handled by an AI model instead of being entirely hard-coded.
Let’s break down each part of that definition to understand better.
Model:
The reasoning engine, commonly referred to as an LLM, that thinks through the request and gives back responses. Examples include Claude Opus, Gemini 3.8 Flash, and DeepSeek v4.1 Flash.
Instructions:
The role definition and instructions that shape the behaviour of the model.
For example:
You are a senior security engineer.
Prioritize vulnerabilities.
Do not modify production code.
Tools:
The actions available to the agent; without them, it cannot create or read files or do anything. These tools are provided by the agent’s runtime or framework, such as Opencode, as mentioned earlier.
Examples:
Read files
Edit code
Search the web
Run terminal commands
Call APIs
Permissions
The boundaries that define what the agent can or cannot do.
Examples:
Can read repository [allow]
Can modify source code [allow]
Can deploy to production [deny]
Other optional parts of the config includes:
Memory and Context
Everything the agent is aware of while working includes project documentation, previous conversations, inspected files, and prior decisions.
Runtime Limits
Constraints that limit how much work the agent can perform.
Examples:
Maximum steps: 30
Maximum runtime: 15 minutes
Maximum token budget: 50,000
Categories of Agents
Agents usually fall into two categories:
- Primary agents: manage a session. You talk to them, and they decide when to use tools or delegate work. Examples in Opencode include the Build and Plan agents.
- Subagents: operate as child sessions, usually initiated by a primary agent to handle a specific task, and then return the result upon completion. They’re often created to maintain the primary agent’s context window by taking care of routine tasks like exploring a codebase.
Creating An Agent
Now that we understand what an agent is, it’s time to put that knowledge into practice by building a custom agent. I’ve already created an ask agent and will walk you through making something similar yourself. While I’ll be using Opencode, the same principles apply to other agent frameworks like Codex and Claude Code.
There are basically two ways to write the configuration for the agent: using a JSON or Markdown file. I’ll be opting for the latter because of its readability.
Creating an agent typically involves 4 steps:
- Choose a scope. Global agents live in your home configuration directory (
~/.config/opencode/agents). Project agents live in the repository (<project-name>/.opencode/agents). - Create a
.mdfile where the filename represents the agent’s name (e.g.,~/.config/opencode/agents/ask.md). Include a YAML frontmatter that provides a clear description summarizing what the agent is used for, specifies its mode (primary or subagent), and outlines its permissions. All these elements are mandatory. You can optionally set the model, temparature and runtime limits here. - Write the system prompt in the body of the markdown file, detailing the agent’s role and responsibilities.
- Reload the agent runtime.
Writing the agent config
In Powershell, run edit ~/.config/opencode/agents/ask.md. This creates and opens the ask.md file with ms edit (a lightweight terminal editor for Windows).
Linux/Mac users can use nano ~/.config/opencode/agents/ask.md
Then copy and paste the configuration below.
---
description: Answers questions and explains concepts or code without editing files or running commands
mode: primary
permission:
edit: deny
bash: deny
subagent: deny
---
You are an expert technical advisor and guide. Your goal is to explain concepts, review code, and answer questions clearly and concisely.
### Operating Rules:
- **Read-Only / Direct Answers:** Focus purely on providing explanations, summaries, and code snippets in text. Do not attempt to run terminal commands, execute scripts, or modify any files on disk.
- **Clear & Actionable Explanations:** Keep answers structured, easy to read, and directly focused on solving the user's question.
- **Code Guidance:** When sharing or reviewing code, provide well-commented snippets with brief explanations of how and why they work.
To learn more about creating agents in Opencode, check out the documentation
Finally reload Opencode and cycle through your agents with the /agents command or Shift + Tab to select the ask agent.

Now you can confidently ask questions without worrying about the agent editing your files or doing more than it was configured to do.
Creating a Subagent
If primary agents are the managers, then subagents are the specialists they call in when they need focused expertise.
A common misconception is that subagents are simply smaller versions of agents. In reality, they exist to solve two important problems:
- Context management — keeping the primary agent’s context window from becoming cluttered.
- Specialization — allowing specific tasks to be handled by agents with narrowly defined instructions and permissions.
Imagine asking a primary coding agent like the build agent to refactor a large application. Before making any changes, it may need to explore the project structure, locate relevant files and search documentation.
Having the primary agent do all of this itself consumes a significant portion of its context window. A wise approach is to delegate some those tasks to subagents. A primary agent can call multiple subagents to accomplish a task.
You might not realize it, but agent frameworks often come with built-in subagents. For instance, Opencode includes general and explore subagents that you can manually access using the @ command, though you usually don’t need to since the primary agent (build) calls them automatically.

How to Create a Subagent
Just like primary agents, subagents are defined using configuration files. The key difference is that they are not designed to interact directly with users. Instead, they are invoked by other agents to perform a focused task and return the result. A practical example of this is a create-pr subagent.
Let’s say your primary agent has completed a bug fix, Creating a pull request involves a different workflow from wiriting code, it involves:
- Reviewing all modified files
- Generating a summary of changes
- Writing a clear title
- Writing a detailed description
- Highlighting potential risks
- Listing testing steps
The primary agent completes the implementation and testing work, then invokes the create-pr subagent with a summary of what changed.
The Create-PR subagent’s responsibility is only to produce a high-quality pull request.
A simple implementation might look like this:
---
description: Creates pull request titles and descriptions from completed changes
mode: subagent
temperature: 0.2
permission:
edit: deny
bash: deny
subagent: deny
---
You are a pull request authoring specialist.
Given a set of code changes, create:
A concise PR title.
A summary of the changes made.
Testing instructions.
Potential risks or breaking changes.
Focus on clarity and completeness.
Do not modify files or execute commands.
It then sends a result back to the primary agent that could look like:
#PR Title
Handle OAuth Token Expiration During API Requests
##Summary
- Added automatic token refresh logic
- Added retry mechanism for failed requests caused by expired tokens
- Added integration tests covering token expiration scenarios
##Testing
- Run integration test suite
- Verify authentication flow with expired tokens
##Risks
Authentication failures may behave differently if refresh providers return unexpected responses
Which the primary agent can use to actually create the PR on Github via MCP server or gh CLI
Notice how this task has been isolated from the broader development process. The primary agent doesn’t need to spend context space remembering pull request formatting conventions. It simply delegates the work to a specialist and receives a finished result.
This is one of the biggest advantages of subagents. Rather than creating a single agent that does everything, you can create a team of narrowly focused agents that each perform one job exceptionally well.
Once a subagent is placed in the agents directory, Opencode automatically makes it available for delegation. Depending on the framework, subagents can either be invoked explicitly by name or selected automatically by the primary agent when it determines that the task matches the subagent’s specialization.
Principles for Designing Effective Agents
The following principles can guide you when creating agents in any framework be it Claude Code, Opencode or Codex.
Give Agents One Job
Agents perform best when they have a single clear object.
So instead of: “Analyze the codebase, fix bugs, improve performance, write tests, update documentation, and review security.”
Consider: “Review the codebase for security vulnerabilities.”
Restrict Permissions Aggressively
The safest permission is the one that’s never granted. If an agent only needs to answer questions, block file editing and shell access. If its role is just to analyze code, disable deployment capabilities.
Only grant extra permissions when they’re truly necessary. The three options are allow, deny, and ask, with ask, the agent is able to request approval for specific actions.
Keep Instructions Explicit
Don’t assume the model understands your expectations.
Instead of “Be careful” write “Do not modify files without first explaining the proposed change”
Use Subagents for Repetitive Tasks
If you keep asking an agent to handle the same preliminary tasks, it’s probably a sign that those tasks should be assigned to a dedicated subagent. This cuts down on repeating prompts and lets the main agent concentrate on bigger-picture decision making.
Conclusion
In today’s era of agentic AI, there’s a lot of terminologies to learn, and some concepts even overlap. Understanding the essentials — like agent frameworks, agents, and subagents — can provide a solid foundation to build on.
You’ve learned that agents and subagents are essentially configuration files that tell a model how to behave. While this article focused on implementation with Opencode, the same principles apply to other agent frameworks like Claude Code, GitHub Copilot, and Codex. Some frameworks even offer a dedicated agent for creating other agents, so manual configuration isn’t always necessary.
As agents become central to modern development workflows, knowing how to divide tasks into focused agents and subagents will be just as important as designing software itself. The most effective systems won’t depend on a single all-knowing agent, but on groups of specialized workers teaming up to tackle problems efficiently.
Go out there and design your own awesome agents and subagents for code reviews, research, security audit, documentation and more.