Logo
hhlee
2026-09-14 21:09:43

Behind
Building an AI Agent from Scratch: Let's Demystify the Magic!

ChatGPT Image 2026년 9월 15일 오전 08_59_51.png

In our previous post, Smart Token-Saving Hacks for AI Agents, we explored quick tips to cut down on tokens right away. This time, we’re peeling back the curtain to look under the hood: why do these tricks actually work?

Think of it like driving a car—once you understand how the engine works, you can get way better mileage. In this post, we’ll walk step-by-step through building a hypothetical automated AI agent so you can grasp the mechanics. Looking beyond just "pushing the buttons" will help you manage tokens and cloud costs much more efficiently!

For this walkthrough, we’ll build an executive assistant agent designed to prep for client meetings. (Heads up to our tech-savvy friends: we’ve simplified quite a few engineering details for non-developers, so please bear with us!)

Requests & Responses: The Basics

This is the simplest, one-dimensional dynamic: you fire off a question, and the AI sends back an answer. It’s the foundational building block, but also the loop running constantly behind the scenes.

Under the hood, an AI model chops your sentence into bite-sized units it can compute—called tokens. Based on everything sent so far plus the tokens it has already generated, it predicts which token is most likely to come next, repeating this split-second process to stitch together a full reply.

However, from a single vague prompt alone, the AI won't magically know who "Company A" is or what kind of meeting you're planning. And unless you explicitly hand it today's date, it won't even know what "next week" actually means on a calendar.

Roles & Ground Rules: Giving Your Agent an Identity

Here’s where things get much more interesting! We can step it up by giving the AI an explicit job description and setting ground rules upfront. In AI lingo, this is what we call the system prompt.

While it won’t solve all your worldly problems just yet, we can pack its role, tone, and constraints right into this prompt. A system prompt serves as a high-level master instruction set attached to every single request. It’s the secret sauce that keeps your AI agent acting consistent, on-brand, and neatly aligned with the format you need every time.

As conversations stretch on, the system prompt acts as an anchor—and its role becomes far more critical. Even when a user's input or earlier messages get a little tangled with incorrect details, your AI service can stay on track, sticking faithfully to its assigned role and rules thanks to that guiding prompt.

In our current example, we're building a full-fledged task agent, so we haven't unlocked its full power just yet. But if you're building a simpler tool? This basic setup alone is enough to launch a standalone app! Think grammar checkers, smart translators, or quick video script polishers. For instance, just set the system prompt to: "You are an expert translator. Translate user text into English using developer jargon. Write in prose, assuming your reader is a junior dev in their first year." Feed in the user's message, call the AI, and boom—you've built yourself a niche translation tool.

Keeping the Context Alive

For a basic app, a clever system prompt with a quick "one question, one answer" loop might do the trick. But real AI agents? They don't just stop at one reply. They gather context, evaluate options, and continuously tweak their work until the goal is achieved. In short: every step of the conversation from start to finish becomes the foundation for what comes next.

We've all seen this flow in everyday chatbots. It might not solve your entire workflow in one magic click (yet!), but through the back-and-forth, the output gets sharper and more tailored with every message.

Now, here's a behind-the-scenes secret that non-developers might not know. With standard stateless APIs, the AI doesn't actually "remember" past chats on its own. With every single new message, the app has to resend the system prompt, the relevant chat history, and any tool outputs back to the model. It feels like the AI just naturally remembers you, but under the hood, the entire backstory is being replayed every time!

That said, you don't always want to dump the entire chat history raw. As conversations get long, smart services will summarize older messages, pull only the crucial snippets, or guide the model to reference past replies. Since the model generates each new answer purely based on the context delivered in that moment, longer contexts can mean higher costs and slower replies. The real magic isn't blindly cramming in every single word—it's curating the exact context the AI needs to make the right call right now.

How Computers and AI Actually Talk to Each Other

Hold up a second! Up until now, models have only answered in human-friendly sentences. But if you want an agent to actually *do* things—like checking a calendar or opening a file—it needs to reply in a language the agent can understand. Something like this...

This is how an agent provides context on what it can do, and the AI model tells it which action to take, using a predefined method within its available tools. Better yet, the AI decides whether to reply to the user in natural language or tell the agent, "Hey, run this first!"

This is a massive game-changer. It unlocks endless possibilities for building AI agents—or even just solving tons of everyday micro-problems. If AI could only speak and respond in natural language, we'd forever be stuck in basic chatbot territory. But thanks to structured responses, computers and AI can finally have real, actionable conversations.

※ Fun fact: I use a bunch of internal tools myself. I used to write them from scratch with pure scripts, but frankly, who has the time? Now, I just let the AI model treat them like skills, pick the right command, and run it. Sure, each call costs a tiny bit, but that cost is basically converging to zero—and the flexibility you get is just unmatched.

※ Recently, a decision-specialized model called jev came out, and it looks perfect for handling all the micro-decisions agents need to make: https://typesafe.ai

The Birth of the Agent

Now, let's circle back to agents and bring this all together. We know how agents talk to models, so we're ready for the grand finale. Looking at it from the user's side, up until now, it was basically just a direct chat between you and the AI model—no real "automation magic." That's because the loop looked a lot like the flow below: the agent was merely passing messages back and forth without doing any of the heavy lifting.

Let’s take this a step further and see how an agent actually talks with an AI model. The agent hands the model a toolkit—along with the exact input formats for each tool—and whenever needed, the model replies not with fluffy natural language, but with a structured tool call.

Once it gets this request, the model figures out what extra info it needs, checks what the agent can actually execute, and hands down the next game plan.

Now we’re finally getting some real work done! Depending on the platform, you might see this backstage banter between the agent and the AI, or it might stay hidden behind the curtain. Either way, as a user, you get the neat, finished answer. It’s not that the AI model suddenly grew its own hands and feet; rather, the model’s brains and the agent’s execution muscles team up to access the outside world and get things done.

While this example was just checking calendars and task lists, the sky's the limit depending on what tools and permissions you grant your agent. Browsing files, verifying rendering results, or fetching media assets across the web—it all happens through this back-and-forth flow. Believe it or not, even the most sophisticated video AI agents start from this exact, elegant foundation.

Wrapping Up

In the beginning, we simply sent a prompt to an AI model and got a sentence back. Then, we added roles, guardrails, and current project context. When the model requests a tool call in a structured format, the agent executes it, passes the results back, and decides the next move. This loop repeats until the mission is complete, seamlessly bridging the AI with the outside world.

So, you don’t need to look at AI agents as mysterious, all-knowing magic that does everything on its own. Behind the scenes, each step is surprisingly straightforward—the real secret sauce lies in how cleverly you chain them together to build a seamless experience.

Looking to save on tokens? Start by trimming the fluff from your context window. Getting wacky, off-target answers? Instead of writing an endless prompt essay, check if a critical piece of info is missing—or if too much irrelevant clutter is distracting your AI editor.

So far, we've peeked under the hood of AI agents to keep those precious tokens from vanishing into thin air. But here's the fun part: mastering this setup does more than just save your wallet—it’s the secret sauce to getting sharper, spot-on responses than anyone else. Plus, you’ll effortlessly know whether to keep rolling with your current chat or start a fresh, sparkling new one.

Go to Article

Join for the newsletter and get the news

E-mails collected are not used for any purpose other than sending newsletters and can be withdrawn at any time

You're subscribed to the newsletter 🎉

We'll come back with useful news
E-mails collected are not used for any purpose other than sending newsletters and can be withdrawn at any time
🗞️ [Update] From Generating Images to Remixing and Video Creation—All in One Place! Just double-click any image in the editor, and boom! You can now jump straight into four powerful AI actions.Create a completely fresh image based on your slide...
[Update] From Generating Images to Remixing and Video Creation—All in One Place!
Junwoo 2026-09-15
🤔 Building an AI Agent from Scratch: Let's Demystify the Magic! In our previous post, Smart Token-Saving Hacks for AI Agents, we explored quick tips to cut down on tokens right away. This time, we’re peeling back the curtain...
Building an AI Agent from Scratch: Let's Demystify the Magic!
hhlee 2026-09-14
🤔 Smart Ways to Save Tokens with AI Agents (Without Losing the Magic) Must be nice if your company covers your token bills, but for a scrappy startup like ours, token cost-efficiency (or simply surviving the invoice) is practicall...
Smart Ways to Save Tokens with AI Agents (Without Losing the Magic)
hhlee 2026-09-11
🤔 Setting Up the Dev & Ops Environment for Our Secret Agents (Yes, You!) Ever wonder how the tech behind seamless AI video editing actually gets built? Today, we’re peeling back the curtain to share how the Stew team sets up and rock...
Setting Up the Dev & Ops Environment for Our Secret Agents (Yes, You!)
hhlee 2026-09-09
🗞️ [Update] Give Stock and Uploaded Photos an AI Makeover with 'Generative Edit' You can now refine and reuse the images you already have—no need to toss them out and start over. With the newly added 'Generate Edit' feature, the image curren...
[Update] Give Stock and Uploaded Photos an AI Makeover with 'Generative Edit'
Junwoo 2026-09-01
🎓 Why You Pick a Shorts Template, Make Three Videos, Then Never Use It Again Search for Shorts templates, and you’ll find an endless supply of free options. But after downloading one and making about three videos, many creators find them...
Why You Pick a Shorts Template, Make Three Videos, Then Never Use It Again
Junwoo 2026-09-01
🎓 Automating shorts is easy—getting them consistently great every time is the real challenge If you search for shorts automation, you’ll find endless workflow tutorials that promise everything at once: script generation, images, voiceover, even upload. ...
Automating shorts is easy—getting them consistently great every time is the real challenge
Junwoo 2026-08-06
🎓 Before You Pick an AI Video Editor, Decide *What* You Actually Want to Make When you read those “Top 5 AI video makers” listicles, they usually bundle all the trendy tools together, compare pros and cons, and highlight key features.But ...
Before You Pick an AI Video Editor, Decide *What* You Actually Want to Make
Junwoo 2026-07-02
[Stop]