KScript docs
EN

The language

Language models: KScript.AI

KScript.AI lets a script ask Claude. The API key comes from the ANTHROPIC_API_KEY environment variable (or a token in ANTHROPIC_AUTH_TOKEN, or the profile ant auth login saved, when the ant command is installed), never from the script. ANTHROPIC_BASE_URL sends requests to another host, such as a proxy. Scripts use claude-opus-5-5 unless they ask for another model, or KSCRIPT_AI_MODEL names one.

<import package="KScript.AI" />

<!-- One question, one answer. -->
<echo>{ @llm('Write a haiku about XML.') }</echo>

<!-- A prompt written as a template, with a system prompt. -->
<let name="customer" value="{ name: 'Ann' }" />
<let name="message" value="'My order is late arrived.'" />
<llm into="reply" system="You answer customer emails politely and briefly.">{$customer.name} wrote: {$message}</llm>

<!-- Structured output: the answer is a map with these fields. -->
<let name="report" value="'The login page spins forever on Safari.'" />
<llm into="ticket" schema="{ category: 'bug|feature|question', urgency: 'int 1-5', summary: 'one line' }">Triage this report: {$report}</llm>
<echo>{$ticket.category} ({$ticket.urgency}): {$ticket.summary}</echo>

<!-- Tools: the model may call your fn functions, and sees what they return. -->
<fn name="order_status" params="id: int" description="The shipping status of an order." value="$id == 42 ? 'shipped' : 'unknown'" />
<llm into="answer" tools="order_status">Where is order 42?</llm>

<!-- A conversation that remembers what was said. -->
<let name="chat" value="@chat('You are a patient maths tutor.')" />
<echo>{ $chat->ask('What is a prime number?') }</echo>
<echo>{ $chat->ask('Is 91 one?') }</echo>
import KScript.AI

// One question, one answer.
echo "{ @llm('Write a haiku about XML.') }"

// A prompt written as a template, with a system prompt.
$customer = { name: 'Ann' }
$message = 'My order is late arrived.'
llm(into: reply, system: 'You answer customer emails politely and briefly.') "{$customer.name} wrote: {$message}"

// Structured output: the answer is a map with these fields.
$report = 'The login page spins forever on Safari.'
llm(into: ticket, schema: { category: 'bug|feature|question', urgency: 'int 1-5', summary: 'one line' }) "Triage this report: {$report}"
echo "{$ticket.category} ({$ticket.urgency}): {$ticket.summary}"

// Tools: the model may call your fn functions, and sees what they return.
/// The shipping status of an order.
fn order_status(id: int) => $id == 42 ? 'shipped' : 'unknown'
llm(into: answer, tools: [@order_status]) "Where is order 42?"

// A conversation that remembers what was said.
$chat = @chat('You are a patient maths tutor.')
echo "{ $chat->ask('What is a prime number?') }"
echo "{ $chat->ask('Is 91 one?') }"
  • llm and @llm(prompt, ...) ask the same question with the same options: system, schema, tools (a list such as [@order_status]), model and max_tokens (16,000 by default). The one difference is where the prompt comes from and where the answer goes: the llm statement takes the prompt as its text and stores the answer in to:, and @llm() takes the prompt as a value and returns the answer, so it fits inside an expression. @llm() takes its options by name: @llm($prompt, system: 'Be brief.', max_tokens: 500).
  • Schemas: each field is required. 'string', 'number', 'int', 'bool' and 'date' name a type, 'int 1-5' gives a range, 'a|b|c' lists the choices, a map is a nested object, ['string'] is a list, and any other text describes a string field.
  • Tools are fn functions. Their params (with types such as id: int) and description tell the model how to call them. If a tool fails, the model is told the error and can carry on.
  • Tests use @fake_llm(replies) instead of Claude: see Faking a language model.
  • Errors have the type AI, so a try with catch e can check $e.type and handle a missing key, a refusal, or an answer cut off by max_tokens. When Claude's safety checks decline a request, the API first retries it on a fallback model.

Edit this page on GitHub