Skip to content

Conversation

@viktorxhzj
Copy link

Context

This PR implements support for OpenRouter's reasoning_details feature, which allows preserving reasoning traces when making API requests. According to OpenRouter's documentation, reasoning_details can be passed back in subsequent API requests to maintain context of the model's reasoning process.

This enhancement enables the system to capture, consolidate, and preserve reasoning traces from models that support this feature, improving the quality and continuity of AI-assisted coding sessions.

Implementation

The implementation spans several layers of the application:

1. Stream Type Definition (src/api/transform/stream.ts)

  • Added new ApiStreamReasoningDetailsChunk type to handle reasoning_details chunks from the API stream
  • This allows reasoning_details to flow through the streaming pipeline

2. OpenRouter Provider (src/api/providers/openrouter.ts)

  • Added logic to detect and yield reasoning_details from the delta stream
  • Integrated with shouldSkipReasoningForModel() utility to skip reasoning for specific models (e.g., Grok-4 which only shows "thinking" without useful information)
  • Imports the model utility for filtering

3. Message Transformation (src/api/transform/openai-format.ts)

  • Implemented consolidateReasoningDetails() function that:
    • Groups reasoning details by index
    • Concatenates text parts within each index group
    • Preserves metadata (signature, id, format, type)
    • Handles both text-based and encrypted reasoning data
  • Modified convertToOpenAiMessages() to extract reasoning_details from message parts and attach them to assistant messages
  • Added proper TypeScript types for ReasoningDetail objects

4. Task Orchestration (src/core/task/Task.ts)

  • Added reasoningDetails array to collect reasoning_details during streaming
  • Added case handler for reasoning_details stream chunks
  • Modified assistant message construction to include reasoning_details in the conversation history
  • Reasoning_details are only added when present (array length > 0)

5. Model Utilities (src/utils/model-utils.ts)

  • Created new utility file with shouldSkipReasoningForModel() function
  • Currently filters out Grok-4 models which don't provide useful reasoning content
  • Designed to be extensible for other models if needed

Trade-offs and Design Decisions

  • Used @ts-ignore comments for reasoning_details properties since they're not part of the standard Anthropic types but are OpenRouter-specific extensions
  • Reasoning_details are optional and only included when present, maintaining backward compatibility
  • The consolidation logic handles both streaming (incremental) and batch reasoning details

Screenshots

before after
N/A - Internal API feature N/A - Internal API feature

This is an internal API enhancement without visible UI changes. The reasoning preservation happens behind the scenes to improve model context.

How to Test

  1. Set up OpenRouter with a reasoning-enabled model:

    • Configure Kilo Code to use OpenRouter as the provider
    • Select a model that supports reasoning_details (e.g., Claude models, DeepSeek R1)
  2. Execute a multi-turn conversation:

    • Start a new task that requires multiple interactions
    • Make initial request that triggers reasoning output
    • Follow up with related requests
  3. Verify reasoning preservation:

    • Check that reasoning_details are being captured in the stream (can add debug logging in Task.ts around line 2103)
    • Verify that subsequent API requests include the reasoning_details from previous turns
    • Confirm that reasoning context improves response quality in follow-up questions
  4. Test Grok-4 filtering:

    • Switch to a Grok-4 model
    • Verify that reasoning_details are skipped (not yielded) for this model
    • Confirm no errors occur
  5. Test backward compatibility:

    • Use providers that don't support reasoning_details (e.g., standard OpenAI)
    • Verify the system works normally without errors
    • Confirm reasoning_details are undefined when not present

@changeset-bot
Copy link

changeset-bot bot commented Nov 8, 2025

⚠️ No Changeset found

Latest commit: ca74feb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chrarnoldus chrarnoldus self-requested a review November 8, 2025 15:10
@chrarnoldus chrarnoldus self-assigned this Nov 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants