AI Agents 123 vues

How to Build a Powerful AI Agent from Scratch

B
Bright Coding
Auteur
How to Build a Powerful AI Agent from Scratch

A chatbot answers questions. An AI agent does things — it checks your email, queries your database, files your reports, and comes back when the job is done. That difference is the single biggest shift in practical AI right now, and you can build a real one from scratch today using free tools. No enterprise platform, no six-figure budget, no mystery.

This guide walks you through the core loop of every AI agent, the tooling that makes it painless, and three real agent patterns you can build this weekend. By the end, you'll understand agents better than most people paying thousands for them.

TL;DR: Key Takeaways

  • The core of an agent is a loop: LLM reasons → picks a tool → executes → observes → repeats until the task is done. That's it.
  • Function/tool calling is the unlock — the model returns structured "call this function with these args," and your code runs it.
  • The 2026 stack: OpenAI/Anthropic tool calling, or open-source with LangGraph/LlamaIndex; n8n for no-code agents.
  • Start with 1–2 tools, not 10. Agents fail by sprawl, not by lack of capability.
  • A reliable agent is 80% guardrails: max steps, explicit "you are done" signals, and human approval gates.

What an Agent Actually Is

Strip away the hype and an agent is embarrassingly simple: a loop where the model decides which of your functions to call. The classic pattern is called ReAct↗ Bright Coding Blog (Reasoning + Acting):

  1. Reason: "The user wants a weekly sales summary."
  2. Act: call query_database('SELECT ...').
  3. Observe: the query returns rows.
  4. Reason again: "I need to summarize this and email it."
  5. Act: call send_email(summary, 'boss@company.com').
  6. Stop when the task's done.

The LLM isn't doing the heavy lifting on its own — you provide the tools, the boundaries, and the definition of "done." The model is the brain; your code is the hands. Build the hands carefully and almost any model can do the thinking.

The Two Ways to Build

Path A: Function Calling with a Cloud API (Fastest)

Both OpenAI and Anthropic support structured tool calling. The model doesn't execute anything — it returns a request to call your function:

from openai import OpenAI

client = OpenAI()

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather for a city",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string"}
            },
            "required": ["city"]
        }
    }
}]

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Is it raining in Lisbon?"}],
    tools=tools,
)

# The model "decides" to call get_weather with city="Lisbon"
print(response.choices[0].message.tool_calls)

Then you run the function and feed the result back. That exchange is the seed of every agent on the planet.

Path B: LangGraph or n8n (Frameworks That Handle the Loop)

Writing the loop by hand is fine for one tool. For branching workflows, state, and error recovery, use a framework:

  • LangGraph (Python↗ Bright Coding Blog) — graph-based agents with explicit state, checkpoints, and human-in-the-loop breaks. The current default for serious Python agents.
  • LlamaIndex — agent framework with strong retrieval/RAG integration.
  • n8n — visual agent builder with an "AI Agent" node that wires an LLM to tools like Google Sheets, Slack, and HTTP requests. No-code-friendly.

Honest guidance: for a first agent, use function calling by hand (Path A). You'll understand the loop forever. Then graduate to a framework when you need state and retries.

Build Your First Agent: The Research Summarizer (Real Code)

Here's a complete, working agent that researches a topic using a web search tool and writes a summary. Adapt the tool to anything you own (database, files, spreadsheets).

import json
from openai import OpenAI
import requests

client = OpenAI()

def search_web(query):
    """Tool 1: a real search (any search API you have, or a free endpoint)."""
    url = "https://api.duckduckgo.com/"
    params = {"q": query, "format": "json", "no_html": 1}
    r = requests.get(url, params=params, timeout=10)
    return json.dumps(r.json())[:1500]  # keep it small for the model

TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "search_web",
            "description": "Search the web and return snippets for a query.",
            "parameters": {
                "type": "object",
                "properties": {"query": {"type": "string"}},
                "required": ["query"],
            },
        },
    }
]

def run_agent(task, max_steps=5):
    messages = [{"role": "user", "content": task}]

    for _ in range(max_steps):  # the guardrail: finite loop
        resp = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=messages,
            tools=TOOLS,
        )
        msg = resp.choices[0].message

        if not msg.tool_calls:
            print("FINAL:", msg.content)  # agent says it's done
            return msg.content

        # Execute the requested tools
        messages.append(msg)  # keep the model's request in history
        for call in msg.tool_calls:
            args = json.loads(call.function.arguments)
            result = search_web(**args)
            messages.append({
                "role": "tool",
                "tool_call_id": call.id,
                "content": result,
            })

    print("Reached max steps — stopping.")
    return None

run_agent("Summarize the top 3 results about 'opencode AI assistant'. List key points as bullets.")

That is a working agent. Study it — everything fancier is this loop with more tools and more guardrails. Notice the two safeguards already in place: max_steps and the check "if no tool calls, the agent is done."

Three Agent Patterns That Actually Work

  1. The Monitor Agent. Runs on a schedule (via cron or n8n), checks a source (price changes, new support tickets, RSS), and alerts you only when something meaningful changes. Simple, high-value, low-risk. The "let me watch this so you don't have to" agent.
  2. The Draft-and-Execute Agent. Drafts an email/PR/code, then sends it only after a human approves. This pattern kills the "rogue agent" fear — the agent never acts without a checkpoint.
  3. The Multi-Tool Pipeline. A data-collection agent: pulls from a database, enriches with an API, writes results to a spreadsheet, and reports. Chain 2–3 tools with clear handoffs.

The Failure Modes Nobody Warns You About

Hallucinated tool calls. The model might invent arguments that don't exist in your data ("I called get_user(id=7) but id 7 doesn't exist"). Validate all inputs to your tools — you're the last line of defense.

Infinite loops that cost real money. Without a step limit, a confused agent can call tools in circles, burning tokens. max_steps and a cost ceiling are non-negotiable.

Context bloat. Every tool result gets stuffed into the conversation. Long runs slow down and get expensive. Truncate results aggressively (like the [:1500] above).

The "good enough to be dangerous" trap. An agent that's 95% reliable will occasionally do something wrong with total confidence. That's why production agents have approval gates for anything consequential.

Tools Comparison

Approach Setup effort Flexibility Best for Cost model
Hand-rolled function calling Low High Learning, simple loops API tokens only
LangGraph Medium Very high Production, stateful agents API + your infra
LlamaIndex agents Medium High Retrieval-heavy agents API + your infra
n8n AI Agent node Low Medium No-code, workflow-friendly Free self-hosted [VERIFY]
CrewAI Low–medium High Multi-agent "teams" API tokens only

Real-World Examples

  1. Support triage agent. Classifies tickets, searches the knowledge base, drafts an answer, and escalates anything it's unsure about. Agents with an explicit "I don't know" path are the ones that survive contact with production.
  2. Meeting-prep agent. Every morning, gathers your calendar, yesterday's notes, and relevant docs, and briefs you before the first call. A schedule trigger + retrieval + a summary tool.
  3. Competitor-watch agent. Monitors competitor pricing pages and changelogs, diffs them, and emails you only when something actually changed. (Beware: scraping terms vary — use official feeds/APIs where possible.)
  4. Invoice-flagging agent. Scans incoming invoices, extracts totals, compares to budget, and flags anomalies to finance. An extension of the data-entry pipeline with judgment on top.
  5. Personal research agent. "Find 3 papers on X, summarize each, and draft a comparison table." One task, three tools, one report. The crowd-favorite weekend project.

The Honest Trade-Offs

Pros:

  • Your boring-but-important work runs without you
  • Understandable architecture: it's just a loop with tools
  • Starts free with open-source models (Ollama) if privacy matters
  • Scales to genuinely complex workflows

Cons:

  • Agents are non-deterministic: same input can produce different paths
  • Debugging is harder than debugging normal code (you can't just "read the flow")
  • Costs scale with reasoning — long agentic loops burn more tokens
  • "Powerful" agents need solid guardrails or they become expensive liabilities

Who this is for

  • Developers and tinkerers who want to understand the tech
  • Small teams automating internal workflows
  • Anyone building private/self-hosted automation

Who it's NOT for

  • People who want a finished product with zero coding (use n8n, but even that needs design)
  • Teams that can't afford an occasional wrong move (start with approval gates)
  • Anyone who'll deploy an agent and walk away — maintenance is part of the job

FAQ

What's the difference between a chatbot and an AI agent? A chatbot generates text responses. An agent can call tools, take actions, and complete multi-step tasks. Same models, different architecture — the agent has hands.

Can I build an agent without coding? Yes — n8n's AI Agent node wires an LLM to tools visually. But you still need to design the workflow, define the tools, and set guardrails. "No-code" reduces the typing, not the thinking.

What are agents bad at? Anything requiring reliability you can't verify, open-ended tasks with no clear "done," and anything where a hallucinated action is costly. Agents are great at contained, well-scoped tasks — and dangerous at fuzzy ones.

Do agents replace software? No. They orchestrate it. Someone still has to build the tools they call. If anything, agents make good, well-structured code more valuable — because the model can finally use it.

Conclusion: Build a Useless Agent on Purpose

Here's the fastest way to learn: build a deliberately trivial agent — one that can check the weather and tell you whether to bring an umbrella. It's "useless" in output, but it teaches you the entire loop: tool definition, tool calling, result handling, and the stopping condition. Then you swap get_weather for something that matters in your work.

Try this: build the research-summarizer agent above tonight, then replace search_web with one tool you actually own — your calendar, your files, your database. Tell me in the comments what your first real agent does. And subscribe — the next guide turns these agents into scheduled, production-grade workers.

Commentaires 0

Aucun commentaire pour l'instant. Soyez le premier à réagir !

Laisser un commentaire