网页 综合 更新时间 2026-07-23 07:03:51

The Dirty Secret Behind AI Agents (Demo 🚀) - DEV Community

自动蜘蛛 dev.to 正文约 4681 字
The Dirty Secret Behind AI Agents (Demo 🚀) - DEV Community
Posted on Jul 23 • Edited on Aug 13
Built in 80 lines without heavy frameworks
For quite a while now, I've had the feeling that AI agents are surrounded by this mystical aura. Nobody really knows what they're doing, they're probably going to take over the world soon, and if you want to build one yourself... well, obviously you need a framework.
What if I told you that's not true? You can build your own AI agent in about 80 lines of code.
Today is my birthday, so as a little gift from me to myself (and to all of you!), I wrote this article. 😄 I hope you'll enjoy it!
Let's get to the point.
Frameworks like LangChain, CrewAI, or Mastra aren't doing magic. They simplify things like conversation memory, tool execution, retries, fallbacks, etc.
Once you understand the underlying mechanism, it becomes much easier to decide when a framework is actually worth using. And even if you end up using one anyway, you'll understand what's happening under the hood.
So I decided to build a small demo and see how little code an AI agent actually needs.
I wrote an AI agent in Node.js in roughly 80 lines of code... okay, okay, the core loop is about 80 lines. There are still tools, a provider abstraction, and a bit of surrounding logic. But come on, "an AI agent in 80 lines" sounds much better. 😄
Here's the repository:
https://github.com/sylwia-lask/code-review-agent
And since it's my birthday... if you enjoy the project, feel free to give it a ⭐. Only if you actually like it, of course. 😄
The application is a simple code review agent. Well... not exactly.
Meet Steve: a software engineer with 15 years of experience reviewing other people's code. Steve doesn't take anything at face value. Sure, he can be a little sarcastic sometimes... but it's hard to argue with his conclusions.
Here's what one of his code reviews looks like:
Or when you have empty diff:
For now, Steve reviews the local Git diff. Which basically means... he's reviewing himself. So I guess I've built a prototype of the famous self-healing agent that, according to @nitsancohen770, is going to take my job one day.
As you can see, I'm basically helping automate myself out of employment. Maybe it's finally time to start thinking about retirement. 😄
In my previous article, I joked about people claiming that an AI agent is nothing more than a while loop. Well... Mine isn't even a while loop. It's a for loop, because I wanted to protect myself from accidentally creating an infinite loop and loosing too much money on tokens. 😄
Okay, to be fair, the loop itself doesn't actually do anything. It's simply responsible for orchestrating the process. But the funny part is that most agent frameworks are doing something remarkably similar under the hood.
The loop itself was trivial to write. The real challenge was exactly where you'd expect it to be.
Building an AI agent starts with... choosing an LLM. 😄 This time I picked the Gemini API because tokens for projects like this are ridiculously cheap. I'm definitely tempted to build the next version using a local model, but... hold on. 😄
I immediately ran into one issue: some Gemini models were overloaded, so I kept getting 503 responses. That meant I had to implement something frameworks usually provide out of the box — a simple retry mechanism.
Mine is intentionally very basic. Production frameworks usually offer much more, like exponential backoff, jitter, or automatic fallback to another model.
The next challenge was, of course, writing the right prompt. After that, everything became surprisingly straightforward.
It's much simpler than you might think.
We send two things to the model:
Here's what the request looks like:
The model can respond in one of three ways:
If it returns plain text, we're done: that's our final code review. If it asks us to call a tool, we move on to Step 3.
For example, we might receive:
Now it's our application's turn.
We execute the requested tool locally. For example, running git diff or reading a file from the repository, and send the result back to the model as another message in the conversation.
The tools available in this demo are:
In other words, everything a good code reviewer needs. 😄
Then we simply go back to Step 2. To avoid getting stuck in an infinite loop, I limit the maximum number of iterations to 10.
There's one more important detail, though.
Take a look at the next request. Notice that we're sending the entire conversation history back to the model:
And... that's it. The LLM decides which tool to use and when it's done. Everything else — executing the tools, handling retries, enforcing iteration limits, and orchestrating the loop — is the responsibility of our application.
访问原文 返回搜索 https://dev.to/sylwia-lask/the-dirty-secret-behind-ai-agents-demo--273d
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。