Intents: Functions You Describe
Here's a function with no code in it:
#[examples([([10], 100), ([50], 400)])]
intent fn price(seats: int) -> int {
'The monthly price for this many seats: $10 each, 20% off at 50 or more.'
}
Call self.price(80) and you get 640. The first time it's called, a model writes price in Stof. The code has to
pass your examples before it's kept, and it runs in a sandbox where it can't touch anything you didn't give it. Every
call after that runs the code it wrote: no model, no tokens, the same answer every time.
That's an intent: a function you describe instead of write. They're new in Stof 0.14.
What happens when you call one
intent fn is ordinary Stof underneath. The body is the intent's text, worked out on each call like any body (one
line is fine: intent fn hello() 'print hello world'), and the function hands it to the document's intent
executor, along with the function and its arguments:
#[intents]
Ai writer: { intent_model: 'anthropic/claude-sonnet-5-5' }
The AI package's Ai is an executor. When an intent hasn't been written yet, its model gets the signature, your
text, your examples, and two tools: define, which takes a draft, checks it, seals it in a sandbox, and runs your
examples against it, and attempt, which runs the draft on arguments of the model's choosing. If a draft fails, the
model sees why and tries again. When one passes, it's kept in the document, next to everything else, and it runs from
then on.
You can read what it wrote (self.writer.intent_source(self.price)), review it, and ship it. Change the intent and
it's written again.
Things it's good at
The function you'd rather describe than write. Normalizing messy input is the classic one:
#[examples([
(['(617) 555-0142'], '+16175550142'),
(['617.555.0142'], '+16175550142'),
(['+1 617 555 0142'], '+16175550142'),
])]
intent fn phone(raw: str) -> str {
'This US phone number in E.164 form: +1 and its ten digits.'
}
The examples aren't decoration: they're the gate. Code that gets one wrong isn't kept.
Rules that live with their data. An intent can be given exactly what it needs, and nothing else:
catalog: { apples: 3, pears: 5 }
fn price(fruit: str) -> int { fruit == 'apples' ? 2 : 4 }
#[reads(self.catalog)]
#[calls(self.price)]
#[examples([([], 26)])]
intent fn total() -> int { 'The cost of all the fruit in the catalog: each count times its price.' }
Its code reads self.catalog and calls self.price(..). Anything else in the document doesn't exist from where it
runs. No network, no files, no environment, a second per call: unless you say otherwise. Give #[calls] a whole
object and it can call everything in it, its children's functions too. And it can't change the document by itself:
only the functions you hand it can. Here's what Claude wrote
for it on a live run:
fn total() -> int {
let sum = 0;
for (const (k, v) in self.catalog) {
sum += (v as int) * self.price(k);
}
sum
}
Things that can't be code. Some intents are judgment calls. #[each] has a model do the intent on every call
instead, and its answer is the result:
#[each]
intent fn reply(ticket: str, tone: str) -> str {
"""Reply to this support ticket in a ${tone} tone, in two sentences or fewer:
${ticket}"""
}
The fun part
Checks can be intents too. Examples catch the cases you thought of. For the rest, write a check, a function that throws when the code is wrong. Or describe one, and have a model write that too:
intent fn checks_median(median: fn) {
'Check that the function returns the median of a list of numbers: throw when it does not.'
}
#[checks(self.checks_median)]
intent fn median(numbers: list) -> float {
'The median of the numbers (the mean of the middle two for an even count).'
}
On a live run, Claude wrote median, then wrote a check for it with eleven cases (odd and even counts, negatives,
duplicates, unsorted input), and the median passed. Intents writing intents that check intents. Somewhere in that
chain there should be an example a person wrote, and then you have something you can trust more than either model
alone.
Build once, ship without a model. Intents are written when they're first called, or ahead of time from a
#[load] function:
#[load]
fn intents() { await self.writer.build_intents(); }
Load the document in CI, export it with the code that was written, and give the Ai write: false where it runs.
Now production runs plain Stof, deterministic and free, and an intent that changed without being rebuilt is an error,
not a surprise model call.
The executor is just an object. Stof itself knows nothing about models. It finds the object marked #[intents]
and calls its intent(func, text, args). So in a test, the executor can be five lines:
#[intents]
canned: {
fn intent(func: fn, text: str, args: list) -> str { `${func.name()} was asked: ${text}` }
}
Or it can be a team's shared library of reviewed code, or a coding agent somewhere else. Whatever writes the function, the document still says what it's for.
Where this goes
The code an intent writes is the how. The intent is the what, and the what is the part that lasts: it's what you review, what you change, and what gets rewritten when the requirements move. We think that's where programming with Stof goes: say what you want, say how you'll know it's right, and let the runtime keep the code honest.
Try an intent in the playground, or read the intents guide.
