Skip to main content

Thinking & Reasoning

Many models support thinking/reasoning capabilities where they show their internal thought process before generating a response. The library provides both a unified interface and provider-specific options.

Checking Reasoning Support

Check if a model supports reasoning:

Reasoning-Capable Models

Models that support reasoning across providers:
  • OpenAI: o1-preview, o1-mini, o3-mini, gpt-5-mini, gpt-5-nano, gpt-5.1-omni
  • Anthropic: claude-sonnet-4-20250514
  • Google: gemini-2.0-flash, gemini-2.5-flash, gemini-2.5-pro
  • xAI: grok-code-fast-1, grok-beta, grok-vision-beta
  • Groq: openai/gpt-oss-20b, openai/gpt-oss-40b
  • Cerebras: gpt-oss-120b
  • OpenRouter: Various models (e.g., z-ai/glm-4.5v)
Use streamSimple and completeSimple for automatic cross-provider compatibility:

Reasoning Levels

Five reasoning levels with automatic provider mapping:
xhigh is only available on OpenAI models with extended reasoning. On other providers, it automatically maps to high.

Custom Token Budgets

Override default token budgets for token-based providers:

Provider-Specific Options

For fine-grained control, use provider-specific options:

Streaming Thinking Content

Thinking content is delivered through dedicated events:

Thinking with Tool Calling

Combine reasoning with tool calls:

Cross-Provider Handoffs

Thinking blocks are preserved when switching providers:

How Cross-Provider Thinking Works

  • Same provider/API: Thinking blocks preserved as-is
  • Different provider: Thinking blocks converted to text with <thinking> tags
  • Tool calls: Preserved unchanged across all providers
  • Text content: Always preserved unchanged

Redacted Thinking

Some providers may redact thinking content for safety:

Complete Example

Full workflow with reasoning:

Cost Considerations

Reasoning tokens are included in output token count:
High reasoning levels can significantly increase token usage and costs. Use medium for most tasks.

Provider Comparison

Best Practices

  1. Start with medium: Balanced performance and cost
  2. Use high for complex tasks: Math, coding, logical reasoning
  3. Avoid xhigh unless necessary: Very expensive, only on OpenAI
  4. Stream for UX: Show thinking in real-time for transparency
  5. Monitor costs: Reasoning can 5-10x your token usage
  6. Use unified API: streamSimple/completeSimple for portability

Next Steps

Streaming

Learn about streaming thinking events

Tools

Combine reasoning with tool calling