AI Agents from Scratch: Core Formula and a Step-by-Step Build Guide

    This guide explains why chat-only LLMs fall short for complex tasks, unpacks the Agent = LLM + Planning + Memory + Tool Use formula, walks through each component (ReAct, RAG-style memory, function calling), and gives beginners a five-step path plus common pitfalls.

    Why you need to understand AI agents

    After large language models arrived, many people got used to solving problems with conversational Q&A: one question, one answer. That is not enough when you want the model to complete a complex task end to end—for example, researching an industry and writing a report, generating code from requirements and self-testing it, or processing a batch of tickets with classification and routing. The work has to be decomposed, context must be remembered, external tools must be called, and the plan must be adjusted from feedback.

    That is why so many beginners look for a solid agent tutorial: they need more than another concept overview. They need something that explains the principles, gives a path, and can be followed by hand. This article covers the principles first, then the roadmap.

    One formula for what an agent really is

    A widely used abstraction in the industry is:

    1. Agent = LLM + Harness = LLM + Planning + Memory + Tool Use

    Broken down:

    • LLM (large language model): the agent’s “brain.” It understands, reasons, and generates, but it only “talks” and cannot execute actions by itself.
    • Harness (orchestration layer): the “execution skeleton” around the LLM so it works in a disciplined way—think when it should think, use tools when it should use tools, and avoid fabricating facts. The harness has three core pieces:
      1. Planning: split a large goal into steps and decide what comes first.
      2. Memory: short-term memory keeps the current task context; long-term memory stores experience and knowledge.
      3. Tool use: gives the model “hands” so it can query databases, call APIs, and run code instead of inventing answers.

    Why a harness is required: a bare LLM has two structural limits. Without external information it tends to hallucinate; it also cannot see the live world, and its knowledge stops at the training cutoff. The harness adds “eyes, hands, and a notebook” so decisions have evidence and actions get feedback.

    The four components, one at a time

    1. Planning: think first, then act

    Planning is task decomposition. Two common patterns:

    • ReAct: thought → action → observation → thought again, in a loop. At each step the model outputs reasoning (Thought), chooses a tool (Action), then continues from the result (Observation).
    • Plan globally, then execute: produce a full task list first, run it item by item, and revise the plan when something unexpected happens.

    Beginner tip: start with ReAct. The structure is simple, easy to debug, and can be expressed in a few dozen lines of pseudocode:

    1. while task is not done:
    2. thought = llm.think(history + current goal)
    3. action = llm.choose_tool(thought)
    4. result = run_tool(action)
    5. append thought/action/result to history

    2. Memory: context for the short term, retrieval for the long term

    • Short-term memory: the conversation context window, holding intermediate results. Context length is limited, so learn to summarize and compress—turn early, lengthy turns into key points.
    • Long-term memory: typically vector storage plus similarity search. Chunk important knowledge into a vector store; at the start of a task, retrieve the most relevant items and inject them into the prompt (the RAG idea).

    3. Tool use: give the model hands

    Tools are callable functions: search, database queries, file I/O, code execution, and so on. The key is to describe tools in a structured way (name, description, parameter schema) so the model knows when and how to call them. A common approach is the vendor’s function-calling convention, with signatures in JSON Schema:

    1. {
    2. "name": "search_order",
    3. "description": "Look up order status by order ID",
    4. "parameters": {
    5. "type": "object",
    6. "properties": { "order_id": { "type": "string" } }
    7. }
    8. }

    4. Model choice and prompt design

    Balance reasoning strength, context length, and cost: use a stronger reasoning model for planning; cheaper, lighter models can handle simple classification and summarization. Prompts should state role, goal, tool list, output format, and constraints (for example: “If information is missing, you must call a query tool; do not answer from memory”).

    A five-step path for complete beginners

    1. Get basic chat working. Call the model API and understand message roles (system/user/assistant) and multi-turn dialogue.
    2. Implement tool calling by hand. Register a simple function (for example, weather lookup). Let the model decide when to call it; you execute it and return the result. This makes the split clear: the model outputs intent, your code executes.
    3. Build a ReAct loop. Run thought–action–observation and ship a small assistant that can chain two or three tools.
    4. Add memory. Session history plus a simple retrieval-based long-term store.
    5. Evaluate and harden. Write test cases for hallucination rate and tool-call accuracy; add retries, result checks, and execution caps (to avoid infinite loops and runaway cost).

    Pitfalls to avoid

    • Do not stack components on day one. Get a minimal loop working, then add memory and tools.
    • Prompts are iterated, not written once. After every failure, inspect the model’s reasoning trace.
    • Limit tool permissions and call counts to reduce accidents and infinite loops.
    • Set an evaluation baseline. Without metrics, agent tuning is guesswork.

    Summary

    Agents are not mysterious: they combine a capable brain with a disciplined skeleton—planning to decompose work, memory to carry context, tools to connect to the real world—so an LLM moves from “can talk” to “can do.” For beginners, the right path is to understand each piece of the formula, then iterate from a minimal closed loop. Follow this route in practice and you can build your first agent in a week or two. To lower infrastructure overhead, you can also start from LLM APIs and related components on platforms such as Baidu AI Cloud and keep your effort on orchestration logic.