KScript docs
EN

KScript.AI

Language models (Claude): @llm, @chat and <llm>.

Import it at the top of a script:

<import package="KScript.AI" />
import KScript.AI

KScript.AI calls a language model, so its examples can't run inside the docs. Copy one into a .ks.xml file and run it with kscript.

ElementsStatements

#<llm>llm

Sends its text as a prompt to a language model (Claude) and stores the answer. @llm(prompt, ...) takes the same options and returns the answer instead, for a prompt that is already a value.

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
into requireda new variable's nameThe variable that receives the answer.
systema string with { }The system prompt.
schemaexpressionAsk for structured output: a map describing the fields, such as { category: 'bug|feature', urgency: 'int 1-5' }. The answer is then a map.
toolsa list of functions: [a, b]<fn> functions the model may call: [@lookup, @weather] (in XML, tools="lookup, weather").fn functions the model may call: [@lookup, @weather] (in XML, tools="lookup, weather").
modela stringThe model to use (default: claude-opus-5-5, or KSCRIPT_AI_MODEL).
max_tokensexpressionThe longest answer, in tokens (default 16000).
Example
<llm into="reply" system="Answer in Markdown.">
  Customer {$customer.name} wrote: {$message}
</llm>
llm(into: reply, system: 'Answer in Markdown.') "Customer {$customer.name} wrote: {$message}"

Commands

Commands are called with @, in any expression: @name(arguments).

#@chat(system?, tools?, model?, max_tokens?: number) → chat

A conversation with a language model that keeps its history: $chat->ask(text). tools, model and max_tokens are given by name, as for @llm().
ParameterTakes
system optionalany value
tools optionalany value
model optionalany value
max_tokens optionalnumber

Arguments can be named: @chat(…, tools: …, model: …).

Example
<let name="chat" value="@chat('You are a helpful assistant.')" />
<echo>{ $chat->ask('Hello!') }</echo>
$chat = @chat('You are a helpful assistant.')
echo $chat->ask('Hello!')

#@fake_llm(replies: list|fn) → fake_llm

For tests: from now on in this run, @llm, llm and $chat->ask() answer from replies instead of asking Claude: a list used in order, or fn(prompt) => answer. A map reply answers a question with a schema. $fake->prompts() lists what was asked.
ParameterTakes
replies requiredlist|fn
Example
<let name="fake" value="@fake_llm(['Paris'])" />
<echo>{ @llm('What is the capital of France?') }</echo>
<echo>{ $fake->prompts() }</echo>
$fake = @fake_llm(['Paris'])
echo @llm('What is the capital of France?')
echo $fake->prompts()

#@llm(prompt, system?, schema?, tools?, model?, max_tokens?: number) → any

Asks a language model (Claude) and returns its answer. The options are the llm statement's, given by name: system (the system prompt), schema (a map of fields; the answer is then a map), tools (<fn> functions it may call), model and max_tokens.Asks a language model (Claude) and returns its answer. The options are the llm statement's, given by name: system (the system prompt), schema (a map of fields; the answer is then a map), tools (fn functions it may call), model and max_tokens.
ParameterTakes
prompt requiredany value
system optionalany value
schema optionalany value
tools optionalany value
model optionalany value
max_tokens optionalnumber

Arguments can be named: @llm(…, system: …, schema: …).

Example
<let name="summary" value="@llm('Summarise in one line: ' + $text, system: 'Answer in plain English.')" />
$summary = @llm('Summarise in one line: ' + $text, system: 'Answer in plain English.')

Methods

On chat values: ->ask(), ->reset()

On fake_llm values: ->prompts(), ->count()