Problem: The model was repeating the same tool calls (e.g., creating "orchestrator" agent multiple times) because:
- Context window compaction removed critical "already done" context
- No mechanism to detect repeated agent creation attempts
- Streaming retries could duplicate content
Solution Applied:
- Added
repetitionPreventiontracking inrun_state.goto count repeated agent.create calls - After 3 attempts to create the same agent, the system returns an error:
"repetition detected: agent 'orchestrator' creation was already attempted 3 times" - This breaks the loop and forces the model to check previous results
Problem: Tool arguments weren't visible during streaming - only summary/error/duration were sent
Solution Applied:
- Added
"arguments"and"output"fields to thetool_endSSE event inengine.go - Frontend already has logic to display these (
chat.js:892) - Now JSON tool calls are fully visible during streaming
The codebase already has excellent subagent infrastructure. Here's how to use it:
- Tool iteration limit approaching (120 iterations)
- Repeated no-progress loops (6+ iterations)
- Context window approaching limit
- Cross-domain tasks
- Long-running analysis
// Delegate a file search task to a specialized subagent
input := tools.AgentRunInput{
CallerAgentID: "orchestrator",
TargetAgentID: "file-searcher", // Use a specialized agent profile
Message: `Search for all files matching pattern "config*.go".
Return only the file paths, one per line.
Do not perform any other actions.`,
TaskID: "search-phase-1",
Source: "subagent/orchestrator",
ThinkingMode: "off", // Reduce token usage
}
result, err := subAgentRunner.ExecuteSubAgent(ctx, input)
// result.FinalText contains the structured output-
Use Task IDs for Traceability
TaskID: "phase-1-analysis" -
Configure Agent Profiles
{ "agents": { "profiles": { "file-searcher": { "enabled": true, "model": {"temperature": 0.1, "max_tokens": 2000} } } } } -
Pass Minimal Context
- Subagents get fresh context windows
- Only pass task description + minimal required context
- Use memory system for cross-agent state
-
Source Tagging
Source: "subagent/orchestrator"Creates clear audit trails in run artifacts
Strategy A: Compaction Before Delegation
// Summarize accumulated tool results
// Extract key findings
// Pass only summary to subagentStrategy B: Parallel Subagents (Map-Reduce)
// Spawn multiple subagents with independent tasks
// Each gets fresh context window
// Results aggregated by parentStrategy C: Sequential Pipeline
// Subagent 1: Phase 1 → writes results to file/memory
// Subagent 2: Phase 2 → reads checkpoint, continues
// Each phase starts with fresh contextinternal/agent/run_state.go: Added repetition detectioninternal/runtime/engine.go: Added arguments/output to tool_end events
internal/tools/agent_tools.go: Subagent tool implementationsinternal/runtime/engine.go: Subagent runner, context managementdocs/MEMORY_SYSTEM.md: Memory system for context persistence