Building with an AI agent¶
Karni is built to be driven by a coding agent, any agent that reads an
AGENTS.md. The agent does not reinvent CRUD, permissions, admin screens or
tests. It runs
karni commands and writes only the domain code. This page is the workflow.
The companion page, Building without one, is the same
project built by hand, command by command.
1. Create the project and open it in your agent¶
karni new wrote AGENTS.md, the file coding agents read at the
repository root. It says: run karni context and karni rules first, run
karni rules --check <file> on every file you write, and karni check
before committing. It lists the procedures for routine tasks, and a request
that starts with /<name>, such as /new-app a ticket shop with online
payments, asks the agent for that procedure. AGENTS.md is generated from
one registry, and karni rules sync refreshes it after an upgrade. If your
tool reads only a file of its own, point it at AGENTS.md.
It opens with the project's kind and its stack, so the agent never asks
which technology to use. karni new asks for the kind in a terminal, and
--kind says it: app (this guide's ticket shop), site for a public
website the client edits in the admin, or site+app. For a website from a
designer's pages, karni new "Crumb Bakery" --kind site --design ../design
builds the site and prints what the agent does next
(start a site project).
2. Say what you want, in domain terms¶
You describe the domain, and the agent runs the generators. A prompt like
Add events with a title, description, start and end time, a venue and an organizer. Organizers manage their own events. Anyone can read published ones. Give me the API, the pages and the tests.
leads a Karni-aware agent to run, roughly:
karni make model Venue name:str:200 city:str:100 capacity:int --with viewset,test --access public-read
karni make model Event title:str:200 description:text starts_at:datetime ends_at:datetime \
"status:choice:draft|published" organizer:fk:User venue:fk:Venue -a --access owned:organizer
karni make migration --app events && karni migrate
karni test
and then to edit one line, the access declaration on EventViewSet, so
that anyone can read published events:
access = Access(read=Where(lambda r: Q(status="published")) | Owned("organizer"), write=Owned("organizer"))
Everything else (serializer, filters, search, pagination, admin, the three
pages, the CRUD test) already exists and is tested. Nothing was pasted from a
template: it is all karni, upgraded with karni upgrade.
3. Let the agent check its own work¶
The agent has three feedback loops that cost no tokens of yours:
karni rules --check events/viewsets.py: every convention violation with its code, line and fix. Agents run it after every file.karni test:assert_crudandassert_isolatedprove the whole API surface and the leak everyone forgets, in two lines per resource.- Errors that teach. Every Karni error names the fix and ends with a
code:
E104: EventViewSet: Owned('organiser') does not lead from Event to the user model … See …/errors/#e104.karni explain E104prints why and how to fix it, offline. An agent reads that and corrects itself instead of searching the docs.
karni check (architecture rules, duplication, the ESLint preset) is the
gate before a commit, and CI runs the same command.
4. Give the agent context, not files¶
karni context prints the live inventory (apps, models, viewsets with their
access, pages, components, hooks, installed modules) in a few hundred
tokens, so an agent starts work without reading the tree. With a history
provider installed (karni doctor lists them), karni context --with-history
<file> adds what changed recently around a file and why.
To keep that history, record your prompts. The built-in provider reads
Gigai Chronicle, which saves every prompt you give an agent inside
your repository: npm install -g gigai-chronicle, then chronicle init. It is
optional, MIT licensed, local-only, and made by Karni's author. See
history providers for how Karni uses it.
Through MCP, the same surface is three tools: karni mcp serves context,
rules and check to any MCP client.
5. What to tell the agent, and what not to¶
Do say:
- the domain: entities, fields, who may read and write what, and which workflows run in the background
- "use
karni make" when it starts writing a serializer or an admin by hand - "run
karni explain <code>" when it loops on an error
Do not ask it to write a serializer, an admin class, a permission class, a
login form, a factory or a seeder. Those are Karni's. The rules (K101–K111,
K201–K205, K301–K308) stop a well-behaved agent from doing it anyway, and
karni check stops a badly behaved one.
6. Project rules of your own¶
karni.rules.py adds project rules the agent sees next to Karni's:
karni rules sync regenerates AGENTS.md with them. See
Karni for AI agents for the full contract: the rule codes,
the procedures (karni rules --procedure new-model), the MCP server and
how enforcement works.