crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[FEATURE] Capture and surface reasoning traces (reasoning_content / thinking blocks) for reasoning models (DeepSeek-R1, Claude 3.7, o-series)

Open Rohitkanithi opened this issue 2 months ago • 3 comments

Feature Area

Core functionality

Is your feature request related to a an existing bug? Please link it here.

NA

Describe the solution you'd like

With the rapid adoption of reasoning models (DeepSeek-R1, Anthropic Claude 3.7 Sonnet with extended thinking, OpenAI o1/o3, and Gemini Flash Thinking), models generate rich chains of thought before executing tools or returning a final answer.

While CrewAI's UsageMetrics correctly tracks reasoning_tokens, the actual reasoning text is silently discarded:

  • In OpenAI-compatible providers (OpenAICompletion), message.reasoning_content (used by DeepSeek-R1, vLLM, Ollama, Together, Groq) is ignored.
  • In Anthropic (AnthropicCompletion), ThinkingBlocks in response.content are captured only for internal continuation state, but omitted from LLMCallCompletedEvent and task results.
  • In crew_agent_executor.py, thought defaults to an empty string "" on native tool calls because it only expected legacy ReAct regex parsing ("Thought: ... \nAction:").
  • TaskOutput and LiteAgentOutput currently have no field to access the agent's chain-of-thought.

Proposed Architecture

  1. Provider-Level Reasoning Extraction:

    • OpenAICompletion: Extract reasoning_content = getattr(message, "reasoning_content", None) or (message.model_extra or {}).get("reasoning_content") from chat completion choices and pass it in LLMCallCompletedEvent.
    • AnthropicCompletion: Extract text from ThinkingBlock instances in response.content and include it in LLMCallCompletedEvent.
    • GeminiCompletion: Extract thought parts where part.thought is True.
  2. Surface in Agent Execution & Output Classes:

    • In AgentAction & AgentFinish: Populate thought with the extracted reasoning text rather than defaulting to "".
    • In TaskOutput & LiteAgentOutput: Add an optional field:
      thought: str | None = Field(
          default=None,
          description="The reasoning trace or chain-of-thought produced by reasoning models.",
      )
      
  3. Console UX (verbose=True): Render the agent's reasoning inside a distinct [Thinking Process] block before tool execution or the final answer so developers can inspect the agent's chain of thought live.

Describe alternatives you've considered

  1. Relying solely on ReAct regex parsing ("Thought: ..." in prompt): Fails completely on modern reasoning models using native function/tool calling, where reasoning is emitted via dedicated API fields (reasoning_content or ThinkingBlock) outside of the text body.
  2. Third-party external tracing (Langfuse/OpenTelemetry only): Leaving reasoning inspection to external APM dashboards leaves local developers and terminal users in the dark when running crews with verbose=True, and prevents downstream tasks or guardrails from validating the agent's reasoning.

Additional context

  • Backward Compatibility: Fully backward compatible. For standard non-reasoning models (GPT-4o, Claude 3.5 without thinking, etc.), thought remains empty/None as it is today.
  • Enterprise Safety: Allows enterprises to audit the agent's internal rationale prior to high-stakes tool execution (e.g. database mutations, financial operations, code edits).

Willingness to Contribute

Yes, I'd be happy to submit a pull request

Rohitkanithi avatar Oct 01 '26 08:10 Rohitkanithi

Hi Maintainers,

I'd love to work on this feature I have already mapped out the implementation across the native providers (OpenAICompletion for reasoning_content, AnthropicCompletion for ThinkingBlock), event bus propagation, and surfacing it cleanly in TaskOutput/LiteAgentOutput with comprehensive unit tests. Could you please assign this issue to me, I'll submit a PR for this.

Thanks

Rohitkanithi avatar Oct 01 '26 08:10 Rohitkanithi

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Oct 31 '26 12:10 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Nov 05 '26 12:11 github-actions[bot]