Building an AI Agent from Scratch: A Practical Guide for Beginners

    This guide explains why beginners often get lost when learning AI agents and introduces a simple mental model: Agent = LLM + Harness, where the Harness handles planning, memory, and tool use. It breaks down each core module, outlines a four-stage learning path, and lists common pitfalls along with three acceptance criteria to help you build agents that reliably get things done.

    1. Why Beginners Get Lost When Learning Agents

    Many beginners open a tutorial and immediately feel overwhelmed: too many concepts, too many terms, and examples that do not run. The root cause is not a lack of intelligence. Most materials assume you already understand that “model capability boundaries” and “engineering orchestration” are two separate things. When these two are mixed together, you get the familiar pattern of “looks like I understand it, but I fail as soon as I try.”

    A clearer mental model is to think of an agent as a formula:

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

    The LLM handles understanding and generation. The Harness, which you can think of as the orchestration and constraint layer, makes the model’s behavior controllable: it reasons when it should reason and calls tools when it should call tools, instead of fabricating results. What the original text calls “no making things up” is, in engineering terms, a requirement that the system replace the model’s free imagination with verifiable external information.

    2. Breaking Down the Three Core Modules

    1. Planning: Splitting a Big Goal into Executable Steps

    Planning is essentially task decomposition and state progression. The most common beginner mistake is asking the model to output the final answer in one shot. A more reliable approach is to have the system maintain an explicit task state, for example:

    1. plan = {
    2. "goal": "Get the weather forecast for a city over the next three days and generate travel advice",
    3. "steps": [
    4. {"id": 1, "action": "resolve_city", "status": "pending"},
    5. {"id": 2, "action": "fetch_weather", "status": "pending"},
    6. {"id": 3, "action": "summarize", "status": "pending"},
    7. ],
    8. }

    Update the state after each step, then decide the next step. The benefits are that failures can be retried, the process is observable, and results are reproducible. Planning does not need to be complex from the start. Implement a “linear flow plus state machine” first, then gradually introduce backtracking and replanning.

    2. Memory: Separating Short-Term Context from Long-Term Knowledge

    Memory is often misunderstood as “stuffing all the history into the prompt.” In practice, it should have at least two layers: short-term memory holds intermediate results of the current task, while long-term memory holds reusable preferences and facts. In engineering, this is usually implemented with structured storage plus retrieval, rather than endlessly concatenating text. The test is simple: a piece of information is worth putting into long-term memory only if it still has value in the next task.

    3. Tool Use: Making Capabilities Come from Outside, Not from Imagination

    Tool calling is the key step that takes an agent from “can chat” to “can get things done.” A usable tool description should include the name, purpose, parameter structure, return format, and failure semantics:

    1. {
    2. "name": "get_weather",
    3. "description": "Get weather data by city and date",
    4. "parameters": {"city": "string", "date": "string"},
    5. "returns": "structured weather data or an error code"
    6. }

    The key points are strict parameter validation, explicit error returns, and clear timeout and retry strategies. The more tools you have, the more you need a unified registration and permission management mechanism.

    3. The Real Value of the Harness: Constraints, Not Free Rein

    The Harness is not a fancy wrapper but a set of constraint mechanisms. It solves at least four problems: first, process constraints that require fetching data before summarizing; second, permission constraints that limit the range of callable tools; third, validation constraints that check the model’s output for format and factual accuracy; and fourth, observability constraints that record the input and output of each step for troubleshooting. Without this layer, the model will use fluent language to mask hollow content.

    4. A Stage-by-Stage Practice Roadmap

    In the first stage, build single-turn tool calling: one tool, one call, one clear result, and get the pipeline working end to end. In the second stage, add a state machine and short-term memory to complete multi-step tasks of three or more steps. In the third stage, introduce retrieval-based long-term memory and multi-tool selection, and add logging plus monitoring and alerting. In the fourth stage, consider concurrency, rate limiting, and cost control.

    When you need to run this as a service, you can rely on general cloud capabilities, such as a container platform for the runtime, a message queue for task decoupling, object storage for intermediate artifacts, and logging services plus monitoring and alerting for observability. Baidu AI Cloud offers corresponding capabilities in these general categories, which you can combine as needed without being locked into a single implementation.

    5. Common Pitfalls and Acceptance Criteria

    Pitfall one: writing longer and longer prompts, which ends up diluting attention. Pitfall two: no failure path design, so the whole flow collapses as soon as a tool errors out. Pitfall three: only looking at the final answer and not the intermediate steps, which makes problems impossible to locate.

    It is recommended to use three criteria for acceptance: whether the same input produces stable results across multiple runs; whether the system degrades gracefully when a tool fails; and whether every step has traceable logs. Once you meet these three, your agent can truly be said to get things done properly.