← Back to Labs

Fix the Subagent Context

agentic architectureFix the Code

This coordinator spawns a subagent but passes the full parent conversation history, which subagents do not inherit. Fix the context passing and ensure the coordinator can spawn subagents.

class ResearchCoordinator {
  private conversationHistory: Message[] = [];

  async delegateToAnalysisAgent(topic: string) {
    const webSearchFindings = await this.runWebSearch(topic);

    // BUG: Subagents do NOT inherit the coordinator's conversation history.
    // Replace with an explicit prompt containing only the relevant findings.
    const subagentPrompt = ;

    const result = await client.messages.create({
      model: "claude-sonnet-4-20250514",
      max_tokens: 4096,
      system: "You are a document analysis specialist.",
      messages: [{ role: "user", content: subagentPrompt }],
      tools: analysisTools,
    });

    return result;
  }
}

// Coordinator agent definition
const coordinatorDef: AgentDefinition = {
  name: "research-coordinator",
  description: "Coordinates research across specialized subagents",
  systemPrompt: "You are a research coordinator...",
  allowedTools: ["WebSearch", "Read", ],
};
Next Exercise →