Skip to main content

Your First Intent

CJ Cummings
Creator and maintainer of Stof, co-founder & CEO of Limitr

What if AI wrote your code at runtime?

intent fn pirate(text: str) -> str { 'rewrite the text like a pirate' }

That's the whole function. No body, just what it should do. The first time it's called, a model writes it, Stof checks it and locks it in a sandbox, and from then on it's normal code. No model, no tokens.

I wanted the first one you try to be something you can paste and run in a minute, so that's what this is. Bring an Anthropic API key.

Python​

pip install stof, save this as pirate.py, and run it with python pirate.py:

import os, time
from pystof import Doc

ANTHROPIC_API_KEY = os.environ.get('ANTHROPIC_API_KEY', 'paste your key here')

doc = Doc()
doc.ai()
doc.parse("""
#[intents]
Ai writer: { intent_model: 'anthropic/claude-sonnet-5-5' }

intent fn pirate(text: str) -> str { 'rewrite the text like a pirate' }
""")
doc.allow_http_hosts(['api.anthropic.com'])
doc.secret('anthropic', ANTHROPIC_API_KEY, ['api.anthropic.com'])

print(doc.call('pirate', ['Hello, my friend! Is that your ship?'])) # Claude writes pirate(), then it runs

start = time.perf_counter()
print(doc.call('pirate', ['Where is the treasure?'])) # runs what Claude wrote, no model
print(f'({(time.perf_counter() - start) * 1000:.1f} ms)')

print(doc.call_source('fn code() -> str { self.writer.intent_source(self.pirate) }'))

TypeScript​

npm install @formata/stof, save this as pirate.mjs, and node pirate.mjs:

import { StofDoc } from '@formata/stof';

const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY ?? 'paste your key here';

const doc = await StofDoc.new();
await doc.ai();
doc.parse(`
#[intents]
Ai writer: { intent_model: 'anthropic/claude-sonnet-5-5' }

intent fn pirate(text: str) -> str { 'rewrite the text like a pirate' }
`);
doc.allowHttp(['api.anthropic.com']);
doc.secret('anthropic', ANTHROPIC_API_KEY, ['api.anthropic.com']);

console.log(await doc.call('pirate', 'Hello, my friend! Is that your ship?')); // Claude writes pirate(), then it runs

const start = performance.now();
console.log(await doc.call('pirate', 'Where is the treasure?')); // runs what Claude wrote, no model
console.log(`(${(performance.now() - start).toFixed(1)} ms)`);

console.log(await doc.callSource('fn code() -> str { self.writer.intent_source(self.pirate) }'));

The first call takes a few seconds, because pirate doesn't exist yet. Claude gets the signature and your one line, writes it in Stof, and defines it. Stof parses that as untrusted code, makes sure the signature is the one you asked for, and seals it so it can't touch the network, files, or anything else you didn't hand it.

The second call is under a millisecond. Nothing asks a model. The function Claude wrote is sitting in the document right next to the intent, and the last line prints it, so go read it. It's yours now. Change the intent's text and it gets written again; leave it alone and it never calls a model again.

The key only goes to api.anthropic.com, and the Stof code uses it without ever being able to read it.

When you want the model every time​

A pirate translator written once is going to be pretty literal, since it has to do the same thing every time. If you actually want every line rewritten fresh, with all the judgment that takes, add #[each] and a model handles every call:

#[each]
intent fn pirate(text: str) -> str { 'rewrite the text like a pirate' }

Same function, same call. You're just paying for a model call each time now.

The boring stuff, written for you​

Where I actually reach for intents is the code I could write, but really don't want to. Parsing the durations people type into a form is a good one. Swap the doc.parse above for this and call seconds:

#[intents]
Ai writer: { intent_model: 'anthropic/claude-sonnet-5-5' }

#[examples([
(['90s'], 90),
(['15m'], 900),
(['1h 30m'], 5400),
(['2 hours'], 7200),
(['1 day, 2 hours'], 93600),
])]
intent fn seconds(text: str) -> int {
'How many seconds this duration is, written the way people write them.'
}

The examples are what make this trustworthy. Every version Claude defines has to give all five answers before it's kept, and when one's off, Claude sees which and tries again. You end up with a parser you didn't write that passes the cases you care about. Then throw a few it hasn't seen at it ('3m 20s', '1 hour 15 minutes') and read what it did.

Messy text into real data​

This is the one I'm most excited about. Every app has a pile of text people typed that should have been data: emails, notes, form fields, support tickets. There's no pattern to write code for, so a model reads each one, and you get a map back:

doc.parse('''
#[intents]
Ai writer: { intent_model: 'anthropic/claude-sonnet-5-5' }

#[each]
intent fn lead(email: str) -> map """
Pull a sales lead out of this email: { name, company, email, seats, budget, timeline },
with null for anything it doesn't say.
"""
''')

lead = doc.call('lead', ["hey, Dana from Harbor Freight here (dana@harborfreight.co). we've got about "
"40 people who'd use this, hoping to roll out in Q1. what's pricing look like?"])
# {'name': 'Dana', 'company': 'Harbor Freight', 'email': 'dana@harborfreight.co',
# 'seats': 40, 'budget': None, 'timeline': 'Q1'}

It's still just a function in a document. You can call it from other Stof, test it, hand it to an agent as a tool, or put a meter on its model calls. And the two kinds mix: a #[each] intent turns the text into data, and a written intent with examples turns that data into a decision, like whether the lead is worth a call today. The judgment happens where it has to, and everything else is code.

It's early, and I'm really curious what people build with this. Come show me on Discord or @cjcbuilds.