Anthropic Messages API client (Claude). Plain and multi-turn chat with a system prompt, tool use, streaming over server-sent events, structured (JSON) output through a forced tool, and extended-thinking blocks. Responses and errors follow the other AI client libraries: a failed call returns Nil and EndPoint->GetLastError() says why. The key comes from SetApiKey, the ANTHROPIC_API_KEY environment variable, or anthropic_api_key.dat in the working directory. Requests go over HTTPS. Compile with -lib anthropic.
Chat
A chat session. It keeps the message history, including tool calls and results.
Example
chat := Chat->New("claude-sonnet-5-5", Nil);
chat->SetSystem("Answer in one sentence.");
chat->Send("Why is the sky blue?")->PrintLine();
chat->Send("And at sunset?")->PrintLine();Operations
- New
- AddMessage
- AddToolResult
- BuildRequest
- GetLastReply
- GetMessages
- GetModel
- Send
- SendJson
- SendReply
- SetMaxTokens
- SetSystem
- SetTemperature
- SetThinking
- SetTopK
- SetTopP
- Stream
AddMessage #
Adds a message to the history without sending it
method : public : AddMessage(role:String, content:String) ~ ChatParameters
| Name | Type | Description |
|---|---|---|
| role | String | "user" or "assistant" |
| content | String | message text |
Return
| Type | Description |
|---|---|
| Chat | self for chaining |
AddToolResult #
Adds the result of a tool call to the history. Results for one reply share one user message, as the API requires, so call this once per ToolUse and then SendReply(Nil, tools).
method : public : AddToolResult(tool_use_id:String, content:String) ~ ChatParameters
| Name | Type | Description |
|---|---|---|
| tool_use_id | String | the id from ToolUse->GetId() |
| content | String | the tool's result |
Return
| Type | Description |
|---|---|
| Chat | self for chaining |
AddToolResult #
Adds the result of a tool call, marking it as a failure when the tool could not run
method : public : AddToolResult(tool_use_id:String, content:String, is_error:Bool) ~ ChatParameters
| Name | Type | Description |
|---|---|---|
| tool_use_id | String | the id from ToolUse->GetId() |
| content | String | the tool's result or error text |
| is_error | Bool | true when the tool failed |
Return
| Type | Description |
|---|---|
| Chat | self for chaining |
BuildRequest #
Builds the request body for the history as it stands, public so it can be inspected without a server
method : public : BuildRequest(tools:Vector<Tool>, stream:Bool) ~ JsonElementParameters
| Name | Type | Description |
|---|---|---|
| tools | Vector<Tool> | tool definitions, or Nil |
| stream | Bool | true to ask for a streamed response |
Return
| Type | Description |
|---|---|
| JsonElement | request JSON |
GetLastReply #
Get the last reply received
method : public : GetLastReply() ~ ReplyReturn
| Type | Description |
|---|---|
| Reply | last reply, or Nil |
GetMessages #
Get the message history
method : public : GetMessages() ~ JsonElementReturn
| Type | Description |
|---|---|
| JsonElement | messages, as sent in the request |
New # constructor
Constructor
New(model:String, key:String)Parameters
| Name | Type | Description |
|---|---|---|
| model | String | model id |
| key | String | API key, or Nil to use GetApiKey() |
Send #
Sends a message and returns the reply text
method : public : Send(message:String) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| message | String | user message |
Return
| Type | Description |
|---|---|
| String | reply text, or Nil with the last error set |
SendJson #
Asks for an answer in a given shape. The model is made to call a tool whose input schema is the one you give, and its input is the answer. One-shot: neither the question nor the answer joins the history.
method : public : SendJson(message:String, name:String, schema:JsonElement) ~ JsonElementParameters
| Name | Type | Description |
|---|---|---|
| message | String | the question |
| name | String | a name for the shape, letters and underscores |
| schema | JsonElement | JSON schema of the answer, an object |
Return
| Type | Description |
|---|---|
| JsonElement | the answer as JSON, or Nil with the last error set |
SendReply #
Sends a message and returns the full reply, including tool calls. When the reply has tool uses, run them, add each result with AddToolResult and call SendReply(Nil, tools) again.
method : public : SendReply(message:String, tools:Vector<Tool>) ~ ReplyParameters
| Name | Type | Description |
|---|---|---|
| message | String | user message, or Nil to send the history as it stands |
| tools | Vector<Tool> | tool definitions, or Nil |
Return
| Type | Description |
|---|---|
| Reply | reply, or Nil with the last error set (the message is then not kept in the history) |
Example
tools := Vector->New()<Tool>;
tools->AddBack(Tool->New("get_weather", "Get the weather for a city", JsonParser->TextToElement("{\"type\":\"object\",\"properties\":{\"city\":{\"type\":\"string\"}},\"required\":[\"city\"]}")));
chat := Chat->New("claude-sonnet-5-5", Nil);
reply := chat->SendReply("What is the weather in Paris?", tools);
while(reply <> Nil & reply->HasToolUses()) {
uses := reply->GetToolUses();
each(i : uses) {
call := uses->Get(i);
city := call->GetArgument("city");
chat->AddToolResult(call->GetId(), "18C and sunny in {$city}");
};
reply := chat->SendReply(Nil, tools);
};
reply->GetText()->PrintLine();SetMaxTokens #
Sets the most tokens a reply may use (default 4096)
method : public : SetMaxTokens(max_tokens:Int) ~ ChatParameters
| Name | Type | Description |
|---|---|---|
| max_tokens | Int | token limit |
Return
| Type | Description |
|---|---|
| Chat | self for chaining |
SetSystem #
Sets the system prompt
method : public : SetSystem(system:String) ~ ChatParameters
| Name | Type | Description |
|---|---|---|
| system | String | system prompt |
Return
| Type | Description |
|---|---|
| Chat | self for chaining |
SetTemperature #
Sets the sampling temperature
method : public : SetTemperature(temperature:Float) ~ ChatParameters
| Name | Type | Description |
|---|---|---|
| temperature | Float | temperature between 0 and 1 |
Return
| Type | Description |
|---|---|
| Chat | self for chaining |
SetThinking #
Turns extended thinking on. The API needs a budget of at least 1024 tokens and a max_tokens above it, so call SetMaxTokens too. While it is on, the temperature, top_p and top_k are not sent (the API rejects both together), and SendJson leaves thinking off for its one request because a forced tool is rejected with it.
method : public : SetThinking(budget_tokens:Int) ~ ChatParameters
| Name | Type | Description |
|---|---|---|
| budget_tokens | Int | tokens the model may think with, 0 to turn thinking off |
Return
| Type | Description |
|---|---|
| Chat | self for chaining |
SetTopK #
Sets top-k sampling: only the k most likely tokens are considered. Unset by default.
method : public : SetTopK(top_k:Int) ~ ChatParameters
| Name | Type | Description |
|---|---|---|
| top_k | Int | number of tokens, at least 1 |
Return
| Type | Description |
|---|---|
| Chat | self for chaining |
SetTopP #
Sets nucleus sampling: only the smallest set of tokens whose probability adds up to top_p is considered. Unset by default, so the API's default applies. Some models refuse a request that sets both the temperature and top_p; change one of them, not both.
method : public : SetTopP(top_p:Float) ~ ChatParameters
| Name | Type | Description |
|---|---|---|
| top_p | Float | threshold between 0 and 1 |
Return
| Type | Description |
|---|---|
| Chat | self for chaining |
Stream #
Sends a message and streams the reply as it is generated
method : public : Stream(message:String, tools:Vector<Tool>) ~ ReplyStreamParameters
| Name | Type | Description |
|---|---|---|
| message | String | user message, or Nil to send the history as it stands |
| tools | Vector<Tool> | tool definitions, or Nil |
Return
| Type | Description |
|---|---|
| ReplyStream | stream, or Nil with the last error set |