Skip to content

Designing Autonomous AI Agents

For programmers who want to build production-grade AI agents


Table of Contents

Part I: Fundamentals

Part II: Practice-first (Build an Agent)

Part III: Architecture and Runtime Core

  • 09. Anatomy of an Agent — Memory, tools, planning, runtime
  • 10. Planning and Workflow Patterns — Plan→Execute, Plan-and-Revise, task decomposition, DAG/workflow, stopping conditions
  • 11. State Management — Tool idempotency, retries with exponential backoff, deadlines, persisting state, task resumption
  • 12. Agent Memory Systems — Two memory horizons (working memory within a run vs. long-term memory across sessions), linear history as an immutable log, prompt cache and why the system prompt is stable, compact vs. condense vs. recall, optional block-based memory
  • 13. Context Engineering — A stable system prefix, a single threshold and a single response (condense with a 1/run limit), usage.PromptTokens as the primary source, over-engineering pitfalls (LayeredContext, message scoring, a dynamic system prompt)
  • 14. Ecosystem and Frameworks — Choosing between your own runtime and frameworks, portability, avoiding vendor lock-in

Part IV: Practice (Case Studies)

Part V: Infrastructure / Platform Security

  • 17. Security and Governance — Threat modeling, tool categorization, prompt injection defense (canonical), tool sandboxing, allowlists, policy-as-code, RBAC, dry-run modes, auditing
  • 18. Tool Protocols and Tool Servers — The tool↔runtime contract at the process/service level, schema versioning, authn/authz

Part VI: Production Readiness

Appendices


Reading Path

  1. Start with the Preface — what an agent is, the equation Brain + Tools + Memory + Planning, and make sure to read the section “Mental Model: The Agent Is a New Employee”. Without it, the security chapters will read like “special LLM machinery” instead of “the common sense you already know.”
  2. Study The Physics of LLMs — the foundation for understanding everything else
  3. Master Prompting — this is the foundation of working with agents
  4. Build a working agent:
  5. Extend its capabilities:
  6. Dive deeper into the architecture:
  7. Practice: Work through the labs in parallel with reading the chapters

For experienced programmers

You can skip the basic chapters and go straight to:

Fast track: the core concepts in 10 minutes

If you are an experienced developer and want to grasp the essentials quickly:

  1. What is an agent?

    • Agent = LLM + Tools + Memory + Planning
    • The LLM is the "brain" that makes decisions
    • Tools are the "hands" that carry out actions
    • Memory is the history and the long-term store
    • Planning is the ability to break a task into steps
  2. The mental model (more important than it seems right now):

    • An agent is a new employee on probation, not "new software".
    • Role-based access, approval for dangerous actions, an audit log, gradual expansion of trust — the same as for a person.
    • Four asymmetries (where the "like a human" model breaks): speed 1000× faster, parallelism, no sense of consequences, prompt injection as social engineering.
    • In detail: Preface → Mental Model.
  3. How does the agent loop work?

    While (task not solved):
      1. Send the history to the LLM
      2. Get a response (text or tool_call)
      3. If tool_call → execute the tool → add the result to the history → repeat
      4. If text → show it to the user and stop
    

  4. Key takeaways:

    • The LLM does not execute code. It generates JSON with a request to execute.
    • The runtime (your code) runs the actual Go functions.
    • The LLM does not "remember" the past. It sees it in messages[], which the runtime assembles.
    • Agent loops typically use a low Temperature (0.0–0.3); reproducibility comes from the schema, validation, and guards in code, not from temperature "determinism".
    • The history is an immutable log; the system prompt is stable across iterations (otherwise the prompt cache is lost — see Ch. 12, Ch. 13).
    • Take the token count from the provider's usage.PromptTokens response, not from your own counters.
  5. A minimal example:

    // 1. Define the tools (the schema is a JSON Schema of the parameters)
    tools := []ToolDef{{
        Name:        "check_status",
        Description: "Check the server status by hostname",
        Parameters:  json.RawMessage(`{"type":"object","properties":{"host":{"type":"string"}},"required":["host"]}`),
    }}
    
    // 2. Request to the model (a single HTTP call to any provider with tool calling)
    resp, err := client.Complete(ctx, Request{
        Model: "glm-5.3",
        Messages: []Message{
            {Role: "system", Content: "You are a DevOps engineer"},
            {Role: "user", Content: "Check the status of the web-01 server"},
        },
        Tools: tools,
    })
    if err != nil {
        return err // client errors are handled by code, not by the model (Ch. 04)
    }
    
    // 3. Check for a tool_call
    if len(resp.Choices[0].Message.ToolCalls) > 0 {
        // 4. Execute the tool (runtime)
        result := checkStatus(resp.Choices[0].Message.ToolCalls[0].Arguments)
        // 5. Add the result to the history (referencing the call ID)
        messages = append(messages,
            resp.Choices[0].Message,
            Message{Role: "tool", ToolCallID: resp.Choices[0].Message.ToolCalls[0].ID, Content: result},
        )
        // 6. Send the updated history back to the model
    }
    

  6. What to read next:

After finishing the core course

After studying chapters 1–16, move on to:


Mapping to the Labs

Guide chapter Corresponding labs
01. The Physics of LLMs Lab 00 (Capability Check)
02. Prompting Lab 01 (Basics)
03. Tools Lab 02 (Tools), Lab 03 (Architecture)
04. Autonomy Lab 04 (Autonomy)
05. Safety Lab 05 (Human-in-the-Loop)
02. Prompting (SOP) Lab 06 (Incident)
06. RAG Lab 07 (RAG)
07. Multi-Agent Lab 08 (Multi-Agent)
09. Anatomy of an Agent Lab 01 (Basics), Lab 09 (Context Optimization)
10. Planning and Workflow Patterns Lab 10 (Planning & Workflow)
11. State Management Lab 10 (Planning & Workflow) — in part
12. Agent Memory Systems, 13. Context Engineering Lab 11 (Memory & Context Engineering)
18. Tool Protocols and Tool Servers Lab 12 (Tool Server Protocol), Lab 13 (Tool Retrieval & Pipelines) — optional
17. Security and Governance — (read as a theory capstone; security practice is built into Lab 02 / Lab 05 / Lab 12)
19–21, 23–25. Production Part — (labs for production topics are planned; the theory is self-sufficient)
22. Prompt and Program Management Lab 01 (Basics) — in part

Note: the chapters and the labs with an LLM client (00–02, 04–11, 13) are revised to the neutral client (llm package in the repo); labs 03 and 12 have no LLM client by design. The examples in the chapters are canonical; work through the labs cross-checking against the canonical material in the chapters.


How to Use This Guide

  1. Read sequentially — each chapter builds on the previous ones
  2. Practice in parallel — after each chapter, complete the corresponding lab
  3. Use it as a reference — come back to the sections you need when working on projects
  4. Study the examples — each chapter includes examples from different domains (DevOps, Support, Data, Security, Product)
  5. Do the exercises — the exercises in each chapter help reinforce the material
  6. Test yourself — use the checklists for self-assessment

Happy learning.