v2026.11.2
All Bundles
Bundle

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.

Inherits: EndPoint

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

AddMessage #

Adds a message to the history without sending it

method : public : AddMessage(role:String, content:String) ~ Chat

Parameters

NameTypeDescription
roleString"user" or "assistant"
contentStringmessage text

Return

TypeDescription
Chatself 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) ~ Chat

Parameters

NameTypeDescription
tool_use_idStringthe id from ToolUse->GetId()
contentStringthe tool's result

Return

TypeDescription
Chatself 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) ~ Chat

Parameters

NameTypeDescription
tool_use_idStringthe id from ToolUse->GetId()
contentStringthe tool's result or error text
is_errorBooltrue when the tool failed

Return

TypeDescription
Chatself 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) ~ JsonElement

Parameters

NameTypeDescription
toolsVector<Tool>tool definitions, or Nil
streamBooltrue to ask for a streamed response

Return

TypeDescription
JsonElementrequest JSON

GetLastReply #

Get the last reply received

method : public : GetLastReply() ~ Reply

Return

TypeDescription
Replylast reply, or Nil

GetMessages #

Get the message history

method : public : GetMessages() ~ JsonElement

Return

TypeDescription
JsonElementmessages, as sent in the request

GetModel #

Get the model id

method : public : GetModel() ~ String

Return

TypeDescription
Stringmodel id

New # constructor

Constructor

New(model:String, key:String)

Parameters

NameTypeDescription
modelStringmodel id
keyStringAPI key, or Nil to use GetApiKey()

Send #

Sends a message and returns the reply text

method : public : Send(message:String) ~ String

Parameters

NameTypeDescription
messageStringuser message

Return

TypeDescription
Stringreply 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) ~ JsonElement

Parameters

NameTypeDescription
messageStringthe question
nameStringa name for the shape, letters and underscores
schemaJsonElementJSON schema of the answer, an object

Return

TypeDescription
JsonElementthe 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>) ~ Reply

Parameters

NameTypeDescription
messageStringuser message, or Nil to send the history as it stands
toolsVector<Tool>tool definitions, or Nil

Return

TypeDescription
Replyreply, 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) ~ Chat

Parameters

NameTypeDescription
max_tokensInttoken limit

Return

TypeDescription
Chatself for chaining

SetSystem #

Sets the system prompt

method : public : SetSystem(system:String) ~ Chat

Parameters

NameTypeDescription
systemStringsystem prompt

Return

TypeDescription
Chatself for chaining

SetTemperature #

Sets the sampling temperature

method : public : SetTemperature(temperature:Float) ~ Chat

Parameters

NameTypeDescription
temperatureFloattemperature between 0 and 1

Return

TypeDescription
Chatself 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) ~ Chat

Parameters

NameTypeDescription
budget_tokensInttokens the model may think with, 0 to turn thinking off

Return

TypeDescription
Chatself for chaining

SetTopK #

Sets top-k sampling: only the k most likely tokens are considered. Unset by default.

method : public : SetTopK(top_k:Int) ~ Chat

Parameters

NameTypeDescription
top_kIntnumber of tokens, at least 1

Return

TypeDescription
Chatself 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) ~ Chat

Parameters

NameTypeDescription
top_pFloatthreshold between 0 and 1

Return

TypeDescription
Chatself for chaining

Stream #

Sends a message and streams the reply as it is generated

method : public : Stream(message:String, tools:Vector<Tool>) ~ ReplyStream

Parameters

NameTypeDescription
messageStringuser message, or Nil to send the history as it stands
toolsVector<Tool>tool definitions, or Nil

Return

TypeDescription
ReplyStreamstream, or Nil with the last error set