Skip to content

Commit 4d5389d

Browse files
committed
Anthropic Claude Platform max_tokens requirement accommodation
1 parent f6ff63c commit 4d5389d

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP32_AI_Connect
2-
version=0.5.0.11
2+
version=0.5.0.12
33
author=AvantMaker <admin@avantmaker.com> (and AI Assistant)
44
maintainer=AvantMaker <admin@avantmaker.com>
55
sentence=A library for ESP32 to interact with various AI APIs (OpenAI, Gemini, DeepSeek, etc.).

src/AI_API_Claude.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

src/AI_API_Claude.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33

44
#include "AI_API_Platform_Handler.h"
55

6+
/**
7+
* AI_API_Claude_Handler - Handler for Anthropic Claude API
8+
*
9+
* IMPORTANT CLAUDE API REQUIREMENTS:
10+
* - The 'max_tokens' field is REQUIRED and cannot be omitted from requests
11+
* - According to Anthropic documentation: https://docs.anthropic.com/en/api/messages
12+
* - All request methods automatically include max_tokens with default value 1024
13+
* - If user sets maxTokens > 0, that value is used instead of default
14+
*
15+
* This handler supports:
16+
* - Regular chat messages
17+
* - Tool calling (function calling)
18+
* - Streaming chat
19+
* - System prompts
20+
* - Custom parameters via setChatParameters()
21+
*/
622
class AI_API_Claude_Handler : public AI_API_Platform_Handler {
723
public:
824
// Constructor and destructor

0 commit comments

Comments
 (0)