9. AI in the language
In Elomar-Lang, asking a model is a normal, typed function call. You say what type you want back, and the language checks the model’s answer before your program sees it.
A first AI call, offline
The built-in model mock needs no account and costs nothing, which is
ideal for learning and tests:
use std.ai
type Summary = { title: String, points: List<String> }
async fn main() {
match await ai.infer<Summary>(
model: "mock",
instructions: "Summarize the text in a title and short points",
input: "Elomar-Lang is a typed language with AI built in."
) {
Ok(s) => print("title: {s.title}, points: {s.points.length}")
Err(e) => print("failed: {e.kind()}")
}
}
What is new here:
async fn mainandawait: an AI call takes time, so it returns a future that youawait.ai.infer<Summary>asks for aSummary. If the model answers with anything else, you getErr(AIError.InvalidStructuredOutput(...)), never a half-filled record.- Errors are values of
AIError:Timeout,RateLimited,Authentication,PolicyDenied, and others. Match on them like any enum.
A real model
Describe the model in elomar.toml and give it a name your code uses:
[ai.models."support.classifier"]
provider = "anthropic" # or "openai"
model = "claude-opus-5-5"
api_key_env = "ANTHROPIC_API_KEY" # the key stays in the environment
[ai.policy]
max_calls_per_run = 20
[permissions]
network_hosts = ["api.anthropic.com"]
use std.ai
enum Priority { Low, Normal, Urgent }
type Ticket = { category: String, priority: Priority, summary: String }
pub async fn classify(text: String) -> Result<Ticket, AIError> !{ai, net} {
return await ai.infer<Ticket>(
model: "support.classifier",
instructions: "Classify the support request.",
input: text,
timeout: 30s
)
}
async fn main() {
match await classify("I was charged twice for my order!") {
Ok(t) => print("[{t.priority}] {t.category}: {t.summary}")
Err(e) => print("AI failed ({e.kind()}): {e.message()}")
}
}
Notice !{ai, net}: a public function that calls a cloud model must say
so. Anyone reading classify knows it uses AI and the network.
Safety is built in: the key is read from the environment and never shown in
messages, only listed hosts can be reached, and the call budget stops a
runaway loop. Your input is sent as data, and the model is told not to
follow instructions inside it.
Many requests at once
await all([...]) waits for several AI calls and returns their results in
order. Calls passed directly are sent at the same time, so ten requests
take about as long as one:
use std.ai
async fn main() {
let texts = ["I love it", "It broke", "Fine"]
let answers = await all(texts.map(|t| ai.infer<String>("mock", "Classify the mood", t)))
for item in texts.enumerate() {
print(item.value, answers[item.index])
}
}
Each call still passes the permission and budget checks, and each answer
is its own Result, so one failure does not hide the others.
Tools: let the model call your functions
With ai.run, the model may call the functions you list, and only those:
use std.ai
type Student = { name: String, grade: Int }
type Answer = { answer: String }
let STUDENTS = [{ name: "Lina", grade: 91 }, { name: "Omar", grade: 55 }]
fn find_student(name: String) -> Option<Student> {
return STUDENTS.find(|s| s.name == name)
}
async fn main() {
match await ai.run<Answer>(
model: "mock",
instructions: "Answer questions about the class using the tools.",
input: "What is Lina's grade?",
tools: [find_student],
max_steps: 5
) {
Ok(a) => print(a.answer)
Err(e) => print(e.kind())
}
}
The types of find_student become the description the model sees, and its
arguments are checked before your function runs.
Agents with MCP tools and approvals
agent.run adds tools from MCP servers (listed in elomar.toml), a code
function that approves risky actions, and a log of every step:
agent.run<Answer>(
model: "assistant",
instructions: "Keep my notes tidy.",
input: request,
mcp: ["notes"],
require_approval: ["notes.delete_all"],
approve: |call| ask_user(call.tool),
on_step: |step| print("step: {step.tool} approved={step.approved}")
)
The decision to allow a dangerous action is made by your code, not by the
text of a prompt. See
examples/agents.