@@ -58,9 +58,11 @@ String AI_API_Claude_Handler::buildRequestBody(const String& modelName, const St
5858 doc[" temperature" ] = temperature;
5959 }
6060
61- if (maxTokens > 0 ) {
62- doc[" max_tokens" ] = maxTokens;
63- }
61+ // IMPORTANT: Claude API requires 'max_tokens' field - it cannot be omitted
62+ // According to Anthropic documentation: https://docs.anthropic.com/en/api/messages
63+ // The 'max_tokens' field is required and cannot be left empty
64+ // Use provided value if > 0, otherwise use default of 1024
65+ doc[" max_tokens" ] = (maxTokens > 0 ) ? maxTokens : 1024 ;
6466
6567 // Add system message if specified (only if user has set it with setTCChatSystemRole)
6668 if (systemRole.length () > 0 ) {
@@ -161,8 +163,10 @@ String AI_API_Claude_Handler::buildToolCallsRequestBody(const String& modelName,
161163 // Set the model
162164 doc[" model" ] = modelName;
163165
164- // Add max_tokens parameter (required for Claude's tool calls API)
165- // Use default value of 1024 if not provided
166+ // IMPORTANT: Claude API requires 'max_tokens' field - it cannot be omitted
167+ // According to Anthropic documentation: https://docs.anthropic.com/en/api/messages
168+ // The 'max_tokens' field is required and cannot be left empty
169+ // Use provided value if > 0, otherwise use default of 1024
166170 doc[" max_tokens" ] = (maxTokens > 0 ) ? maxTokens : 1024 ;
167171
168172 // Add system message if specified (only if user has set it with setTCChatSystemRole)
@@ -442,8 +446,10 @@ String AI_API_Claude_Handler::buildToolCallsFollowUpRequestBody(const String& mo
442446 // Set the model
443447 doc[" model" ] = modelName;
444448
445- // Add max_tokens parameter (required for Claude's tool calls API)
446- // Use default value of 1024 if not provided
449+ // IMPORTANT: Claude API requires 'max_tokens' field - it cannot be omitted
450+ // According to Anthropic documentation: https://docs.anthropic.com/en/api/messages
451+ // The 'max_tokens' field is required and cannot be left empty
452+ // Use provided value if > 0, otherwise use default of 1024
447453 doc[" max_tokens" ] = (followUpMaxTokens > 0 ) ? followUpMaxTokens : 1024 ;
448454
449455 // Add system message if specified (only if user has set it with setTCChatSystemRole)
@@ -759,9 +765,11 @@ String AI_API_Claude_Handler::buildStreamRequestBody(const String& modelName, co
759765 doc[" temperature" ] = temperature;
760766 }
761767
762- if (maxTokens > 0 ) {
763- doc[" max_tokens" ] = maxTokens;
764- }
768+ // IMPORTANT: Claude API requires 'max_tokens' field - it cannot be omitted
769+ // According to Anthropic documentation: https://docs.anthropic.com/en/api/messages
770+ // The 'max_tokens' field is required and cannot be left empty
771+ // Use provided value if > 0, otherwise use default of 1024
772+ doc[" max_tokens" ] = (maxTokens > 0 ) ? maxTokens : 1024 ;
765773
766774 // Add system message if specified
767775 if (systemRole.length () > 0 ) {
0 commit comments