-
Notifications
You must be signed in to change notification settings - Fork 0
Add ThinkingParameters and Interleaved Thinking support to Microsoft.Extensions.AI ChatOptions #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ThinkingParameters and Interleaved Thinking support to Microsoft.Extensions.AI ChatOptions #3
Conversation
Co-authored-by: PederHP <[email protected]>
Co-authored-by: PederHP <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example token values are more than Sonnet 3.7 allows. I think we should use <= 4096 for max_tokens and then less than equal that for the thinking budget.
There's a detail where if this beta header is passed: interleaved-thinking-2025-05-14 - then thinking tokens is allowed to be higher than max tokens. But it isn't currently. Can you make that an option?
However it should be noted that:
On 3rd-party platforms (e.g., Amazon Bedrock and Vertex AI), if you pass interleaved-thinking-2025-05-14 to any model aside from Claude Opus 4.1, Opus 4, or Sonnet 4, your request will fail.
On direct Anthropic API access it's ok to pass it always, but it cannot be auto-passed for the others. Can you take a look at making it configurable, and then for now it's up to the developer to ensure it is only passed with compatible models for those providers.
I am thinking a WithInterleavedThinking() extension on ChatOptions() would be a super cool way to configure it if possible.
|
@copilot I have some review comments |
Co-authored-by: PederHP <[email protected]>
I've implemented all the requested changes in commit 2e59561: ✅ Fixed token values: Updated all examples to use 4096 for max_tokens and appropriate thinking budget (≤ max_tokens for regular thinking) ✅ Added WithInterleavedThinking() extension: Created fluent extension methods that enable the ✅ Dynamic header support: Automatically adds the interleaved thinking beta header only when needed, without affecting global client configuration ✅ Platform compatibility handling: Added documentation noting that on 3rd-party platforms (Bedrock, Vertex AI), this only works with Claude Opus 4.1, Opus 4, or Sonnet 4 models Usage examples: // Regular thinking (budget ≤ max_tokens)
ChatOptions options = new()
{
ModelId = AnthropicModels.Claude37Sonnet,
MaxOutputTokens = 4096,
}.WithThinking(4000);
// Interleaved thinking (budget can exceed max_tokens)
ChatOptions options = new()
{
ModelId = AnthropicModels.Claude37Sonnet,
MaxOutputTokens = 4096,
}.WithInterleavedThinking(8000); // Works with beta headerAll 20 tests pass, including comprehensive coverage of the new functionality.
I've addressed all the review comments from your previous feedback in commit 2e59561. The implementation includes proper token values (4096 max_tokens), the requested WithInterleavedThinking() extension method, and dynamic beta header support for enhanced thinking capabilities. |
Summary
This PR adds native support for ThinkingParameters and advanced Interleaved Thinking capabilities in the Microsoft.Extensions.AI (MEAI) compatible API through clean extension methods. Previously, users had to use the verbose
RawRepresentationFactoryworkaround to enable extended thinking functionality with Claude 3.7 Sonnet and newer models.Problem
The ThinkingParameters feature was fully supported in the basic Anthropic.SDK API but only accessible in MEAI through a cumbersome workaround:
Solution
Added fluent extension methods that provide a clean, intuitive API with support for both regular and interleaved thinking:
Key Features
ChatClientHelperRawRepresentationFactoryapproach still worksinterleaved-thinking-2025-05-14beta header when neededImplementation Details
Core Components
ChatOptionsExtensions.cs- Extension methods for ChatOptions:WithThinking(int budgetTokens)- Simple fluent method for regular thinkingWithThinking(ThinkingParameters)- Advanced method with custom parametersWithInterleavedThinking(int budgetTokens)- Enables interleaved thinking modeWithInterleavedThinking(ThinkingParameters)- Advanced interleaved thinkingGetThinkingParameters()- Retrieval methodEnhanced
ThinkingParameters.cs- Added support for interleaved thinking:UseInterleavedThinkingproperty to track modeUpdated HTTP Infrastructure - Dynamic header support:
BaseEndpoint.HttpRequestRaw()to accept additional headersMessagesEndpointto detect interleaved thinking and add beta headerUpdated
ChatClientHelper.cs- Automatic parameter mapping:Comprehensive Testing - Full test coverage:
Usage Examples
Basic Usage
Advanced Usage with Interleaved Thinking
Custom ThinkingParameters
Platform Compatibility
Breaking Changes
None. This is a purely additive change that maintains full backward compatibility.
Testing
This enhancement significantly improves the developer experience for MEAI users wanting to leverage Claude's extended thinking capabilities, including the advanced interleaved thinking mode, while maintaining the library's commitment to clean, intuitive APIs.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.