跳到正文
DEV Community· Davis George·· 54 分鐘前AI 評分66

如何在 30 分鐘內建立首個 AI 智能體,並補上多數教學略過的 3 個部分

Build Your First AI Agent in 30 Minutes (and the 3 Parts Most Tutorials Skip)

AI 導讀

Davis George 示範如何在 30 分鐘內建立一個發布説明 AI 智能體,讓它讀取 git 記錄並草擬 RELEASE_NOTES.md。流程以 3 個工具和 10 步上限運作,並透過 AGENT.md、MEMORY.md 和操作日誌限制權限、保存修正及追蹤每次執行。他建議先在測試儲存庫確認智能體只用獲授權工具、只更改指定檔案,並確保每條説明可追溯至真實提交。

正文

Build Your First AI Agent in 30 Minutes (and the 3 Parts Most Tutorials Skip)

Every feed this week has a "build an AI agent in 30 minutes" post. Most of them hand you a prompt to paste and a screenshot of the result. That is not an agent — that is a prompt with good lighting.

An agent is a loop: the model decides what to do next, uses a tool, looks at the result, and repeats until the job is done or it runs out of steps. If you understand that loop, you can build one in half an hour. If you don't, no pasted prompt will save you.

Here is a small one you can actually build, with a job narrow enough to finish: a release-notes agent. It reads yesterday's git history and drafts release notes into a file. Nothing more. That narrowness is the whole trick.

Step 1: Define one job, in one sentence

Write the job down before you write any code. If you can't state it in one sentence, the agent can't either.

"Read the commits since the last release tag, group them by what changed, and write a draft RELEASE_NOTES.md. Do not commit, push, or edit any other file."

That last clause matters as much as the first. An agent without a boundary will eventually do something you didn't ask for. Write the boundary into the instructions, and then enforce it again in code (Step 4).

Step 2: Give it context it can actually use

Models don't need a 200-line system prompt. They need three things:

  1. The job — the sentence above.
  2. The repo — the agent should read the git log itself rather than you pasting it in. That is what tools are for.
  3. Your conventions — a short file the agent reads on every run. Call it AGENT.md:
# Release notes conventions
- Group changes under: Features, Fixes, Internal.
- One bullet per commit; keep each under 20 words.
- Never invent a feature that isn't in the commits.
- If a commit message is unclear, write "Unclear — needs human review"
  instead of guessing.

This file is the cheapest leverage in the whole build. When the output is wrong, you usually fix it here, not in the code.

Step 3: Give it three tools, not thirty

A tool is just a function the model can call, described in plain language. Three is enough for this job:

TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "git_log",
            "description": "List commits since a tag. Returns one line per commit.",
            "parameters": {
                "type": "object",
                "properties": {
                    "since_tag": {"type": "string", "description": "e.g. v1.4.0"}
                },
                "required": ["since_tag"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "read_file",
            "description": "Read a file from the repo.",
            "parameters": {
                "type": "object",
                "properties": {
                    "path": {"type": "string"},
                },
                "required": ["path"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "write_notes",
            "description": "Write the draft release notes to RELEASE_NOTES.md.",
            "parameters": {
                "type": "object",
                "properties": {
                    "content": {"type": "string"},
                },
                "required": ["content"],
            },
        },
    },
]

Notice what is not here: no commit tool, no push tool, no shell tool. Every tool you add is a permission you grant. Start with the smallest set that can finish the job, and add tools only when a real run proves you need one.

Step 4: The loop — the whole agent in 30 lines

This is the part the pasted-prompt tutorials skip. The loop is the agent.

from openai import OpenAI

client = OpenAI()  # any OpenAI-compatible API works here
MAX_STEPS = 10

def run_agent(job: str):
    messages = [
        {"role": "system", "content": open("AGENT.md").read()},
        {"role": "user", "content": job},
    ]
    for step in range(MAX_STEPS):
        resp = client.chat.completions.create(
            model="your-model", messages=messages, tools=TOOLS
        )
        msg = resp.choices[0].message
        messages.append(msg)
        if not msg.tool_calls:
            return msg.content  # done: the model answered instead of acting
        for call in msg.tool_calls:
            result = execute_tool(call)  # your code runs git_log/read_file/write_notes
            messages.append(
                {"role": "tool", "tool_call_id": call.id, "content": result}
            )
    return "Stopped after 10 steps — check the log."

Three details make this safe enough to run on a real repo:

  • MAX_STEPS is a hard stop. A model that keeps calling tools forever is not working hard; it is stuck. Ten steps is plenty for this job.
  • The model never touches the repo directly. It asks for git_log; your code runs the command. That is the boundary from Step 1, enforced where it counts.
  • Every step is logged. Append each tool call and result to a file. When the output is wrong, the log tells you whether the model misread the commits or your tool returned garbage. Without the log, you are guessing.

Step 5: Give it memory (the part that makes it better next week)

A one-shot agent starts from zero every run. A useful one remembers corrections. The simplest memory that works: after each run, append what you fixed to a file, and load that file in the system prompt next time.

# MEMORY.md
- 2026-11-20: "Dependency bumps go under Internal, never Features."
- 2026-11-21: "If two commits touch the same file, merge them into one bullet."

Two rules, written in plain language, and the agent stops making the same two mistakes. This is the same idea as a project's CLAUDE.md or a skills file in tools like Claude Code: conventions and corrections, stored as text, read on every run. No database, no embeddings, no vector store — a text file is enough until your memory file outgrows a single screen.

Step 6: Test it on a repo you don't care about

Before pointing this at a real project, run it against a small test repo where you already know what the release notes should say. Then check three things:

  1. Did it use only the tools you gave it? Read the log.
  2. Did it change only RELEASE_NOTES.md? Check git status.
  3. Is every bullet traceable to a real commit? If one isn't, the model invented it — and your AGENT.md needs a sharper rule, not a bigger model.

If all three pass, you have an agent. If any fails, you have a demo.

What 30 minutes actually buys you

Thirty minutes gets you a working loop, three tools, and one narrow job that runs end to end. It does not get you a general-purpose agent, and anyone selling you one in 30 minutes is selling you a prompt.

The things that take longer — and are worth the time — are the unglamorous parts: the conventions file, the memory file, the step log, and the test run on a repo you don't care about. Those are what turn a clever demo into a tool you run every week without watching it.

Start narrow. One job, three tools, ten steps. Get that right, and the second agent takes 15 minutes.

— Davis George · zhuan.xyz

來源:DEV Community · dev.to