I built an AI agent in 12 lines of code
No framework. No server. Just one function that talks back.
The word "agent" sounds like a big system. Queues, planners, a control loop, a diagram with twelve boxes. So you put off building one.
Here's the secret: the core of an agent is one function. You send it a prompt. It sends back an answer. Everything else — memory, tools, a chat app to talk to it — is something you bolt on later.
Let me show you the whole thing.
The code
This lives in one file, src/claude.ts:
import { query } from "@anthropic-ai/claude-agent-sdk";
export async function ask(prompt: string): Promise<string> {
let reply = "";
for await (const event of query({ prompt, options: { model: "claude-sonnet-4-6" } })) {
if (event.type === "result" && event.subtype === "success") {
reply = event.result;
}
}
return reply;
}
That's it. That's an AI agent. I call mine zepto-claw — zepto because it's the smallest unit I could make and still have it talk back.
How it works
query() sends your prompt to the agent and streams back a series of events as it
thinks. We don't care about most of them. We wait for the one that says
result with success, keep its text, and return it.
Prompt in. Stream of events. Grab the result. No server, no framework — a function that asks and returns the answer.
Try it yourself
- Install the SDK:
npm i @anthropic-ai/claude-agent-sdk - Set your API key.
- Paste
ask()into a file and call it:console.log(await ask("what's 2+2?"))
Wire ask() to a terminal prompt and you can talk to it live. Type a question,
get an answer. It's alive.
One honest caveat: it forgets everything between messages. Every call is a blank slate. That's on purpose — we're building small first, and memory is its own piece.
E02 — Give it a memory
Right now the claw forgets you the moment it answers. Next we give it a memory so it remembers the conversation.
Watch the full build on YouTube.