Skip to content

agenttool: Run() leaks thinking parts into tool result #697

@dmora

Description

@dmora

Description

agenttool.Run() collects all Part.Text from the sub-agent's final response without checking Part.Thought. When thinking models (Gemini 2.5/3 with IncludeThoughts: true) are used as sub-agents, their internal reasoning tokens leak into the tool result that the calling agent sees.

Root cause

tool/agenttool/agent_tool.go lines 223-228:

for _, part := range lastContent.Parts {
    if part != nil && part.Text != "" {
        textParts = append(textParts, part.Text)
    }
}

No check on part.Thought. Compare with genai.GenerateContentResponse.Text() (types.go:3148-3151) which correctly skips thought parts:

if part.Thought {
    continue
}

Impact

  • Thinking tokens leak into tool results — the root agent sees the sub-agent's internal reasoning mixed into the response text
  • OutputSchema validation can fail — when thought text is concatenated before JSON output, unmarshalling breaks
  • Token waste — the root agent processes thinking tokens it shouldn't see, inflating context

Related

Fix

Submitted in #696 — adds && !part.Thought to the text extraction loop, matching genai.Text() behavior.

Metadata

Metadata

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions