Dynamic Sub Agents in Claude Code
Claude Code is one of the most popular AI assistants for software engineers. It helps write code, tests, and does a lot of nice things to speed up the creation and delivery of software products. In the last week, Anthropic has added a sub-agent feature. I've decided to take a look at this feature, but from a different approach.
In the latest version of Claude Code, you can create your sub agents - specialised AI assistants with a focus on one particular aspect of software development. In a real-world scenario, we can make a code writer, a reviewer and a security sub agent. These agents won't share context, but will be orchestrated by Claude Code to solve the user's request. All of these agents will contribute to the final solution, if needed. By default, one sub-agent is always available.
I'm pretty sure that this feature has great potential to transform the way we write software. The problem is that you don't always know what agent you need to solve your particular request. In the software development world, we will soon have a gold standard of sub agents that everybody needs, but Claude Code is used not only by software developers.
I've decided to build a chat app from Claude Code. The app's idea is to answer open-ended questions from various perspectives. If I have a question regarding space exploration, I want to hear the opinions of people who have been astronauts, rocket builders, and physicists. Something like what Character AI is doing.
From a technical point of view, we need to re-purpose the Claude code from writing code to answering questions, create new sub-agents on request, and use these new sub-agents in response. In terms of Claude Code, every perspective is a sub-agent.
Re-purposing the Claude Code is easy - we create CLAUDE.md and write 'You are a helpful AI assistant that uses a panel of agents to answer questions...'. Basically, we are updating Claude's memory. The alternative solution is to update a system prompt --append-system-prompt flag.
Creating new sub agents is not hard. Sub agents must be placed in the agents sub-directory of the .claude folder. The file format is MD file with meta. In the meta, we specify the sub-agent name, description and tools available to it. In our case, all perspectives/agents should be able to use the web, so as tools we will have WebSearch, WebFetch. As a prompt for the agent, we will provide a description of a person, such as background, experience, communication style, etc. New agent creation is a part of the CLAUDE. md file, in this file, we instruct Claude Code to use agents to answer a question or create new ones when needed.
The fun part is to make Claude Code load agents dynamically. As of the time of this writing, Claude Code does not load agents on request; it appears to do so only at startup. In our case, it's a blocker. We've just created new perspectives/personas to answer a question, but can not call them straight away. We have several options to solve this: restart the app, re-read new agents into memory and hope it uses them, call Claude from Claude, or use one more agent.
Restarting the app to load agents into memory is an easy option, but we need to do it manually as a user.
Re-read the new agents we can implement by adding a special instruction to CLAUDE.md: 'Before using the agent, you must re-read it from the file.' The downside of this option is that we do not use sub agents, we use the default agent, not a newly created one.
As for calling Claude, from Claude. It's an interesting approach, we do this by adding to CLAUDE.md something like this: 'To generate the final answer, you must call: claude -p "initial question" '. The -p switch tells Claude Code to print the response immediately. The problem is that we do not see what is happening in the second Claude instance.
The option that works for me is to call another sub agent - impersonator. The idea of Impesonator is to indirectly call the agent we need, but which is not yet loaded by Claude. The impersonator can read from .claude/agents folder with available tools: WebSearch, WebFetch, Read, Grep, Glob, Bash. Having the content in memory, it has pretty simple instructions: find the agent, read the description, and use the description to answer the question.
On the first run, the Claude Code has access to only one custom sub agent (impersonator), custom memory with instructions on how to process the user's request, create new agents and call the impersonator if needed. The Claude Code creates the first batch of sub agents dynamically and uses an impersonator to call them all. Once the perspectives are received, Claude generates the final answer. On the second run, the Claude Code will re-use custom agents directly without generating new ones or calling an impersonator.
A few observations. I think Claude Code should add it as a standard feature: load agents on request or check the agents folder more frequently. All agents are executed in parallel. The quality of answers is higher in the multi-agent mode compared to the default settings. Even one sub agent can be executed in parallel if needed.



Resources:
Sub Agents - https://docs.anthropic.com/en/docs/claude-code/sub-agents
Dynamic Sub Agents Repo - https://github.com/shchahrykovich/dynamic-sub-agents






