Multi-agent integration

One agent can do everything, but it also does everything at once. A research agent with every shell tool in the world is overkill. A planning agent with a shell is dangerous. Specializing into a few smaller agents is how real systems scale. Give each one a narrow SOUL and its own toolbox, then route messages to the right one.

Route by prefix, share the memory

Each agent has its own identity and tools. They all read and write the same memory directory so knowledge flows between them.

10-multi-agent-integration/mini-openclaw.py
python
AGENTS = {
    'general': {
        'soul_path': 'souls/general.md',
        'tools': [read_file, write_file, save_memory, memory_search],
    },
    'research': {
        'soul_path': 'souls/research.md',
        'tools': [web_search, read_file, save_memory, memory_search],
    },
    'code': {
        'soul_path': 'souls/code.md',
        'tools': [run_command, read_file, write_file, save_memory, memory_search],
    },
}

def resolve_agent(text: str) -> tuple[str, str]:
    if text.startswith('/research '):
        return 'research', text[len('/research '):]
    if text.startswith('/code '):
        return 'code', text[len('/code '):]
    return 'general', text

A config dictionary defines each agent. A tiny resolver maps the message prefix to the right one and strips the prefix out.

10-multi-agent-integration/mini-openclaw.py
python
def run_multi_agent_turn(user_id: str, text: str) -> str:
    agent_name, cleaned_text = resolve_agent(text)
    config = AGENTS[agent_name]

    # Sessions are per agent, memory is shared
    session_key = f'{user_id}:{agent_name}'
    soul = Path(config['soul_path']).read_text()

    return run_agent_turn(
        user_id=session_key,
        text=cleaned_text,
        soul=soul,
        tools=config['tools'],
    )

Sessions are keyed per agent so each one keeps its own train of thought. Memory lives in a shared directory so facts saved by one agent are visible to all of them.

A big SOUL asks the model to hold a dozen behaviors in its head at once, which it does poorly. Narrow agents with focused SOULs perform better on their own job and give you a smaller blast radius for tools. A code agent can have a shell, a research agent cannot. That separation would be clumsy to express inside one mega-prompt.

Ordering exercise: Order the full pipeline of one multi-agent turn

Loading practice…

Checkpoint: The full assistant in your head

Loading practice…