Designing Autonomous AI Agents¶
For programmers who want to build production-grade AI agents
Table of Contents¶
Part I: Fundamentals¶
- 00. Preface — How to read this guide, prerequisites, what an agent is
- 01. The Physics of LLMs — Tokens, context, temperature and its limits, probabilistic nature
- 02. Prompting as Programming — ICL, Few-shot, CoT, structuring tasks, SOP
Part II: Practice-first (Build an Agent)¶
- 03. Tools and Function Calling — JSON Schema, validation, error handling, the tool↔runtime contract
- 04. Autonomy and Loops — ReAct loop, termination, loop prevention, observability
- 05. Safety and Human-in-the-Loop — Confirmation, clarification, tool categories, prompt injection
- 06. RAG and the Knowledge Base — Chunking, retrieval, grounding, search modes, limits
- 07. Multi-Agent Systems — Supervisor/Worker, context isolation, task routing
- 08. Evals and Reliability — Evals, prompt regressions, quality metrics, test datasets
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.PromptTokensas 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)¶
- 15. Real-World Case Studies — Examples of agents across domains (DevOps, Support, Data, Security, Product)
- 16. Best Practices and Areas of Application — Best practices for building and maintaining agents, areas of application
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¶
- 19. Observability and Tracing — Structured logging, tracing agent runs and tool calls, metrics, log correlation
- 20. Cost & Latency Engineering — Token budgets, iteration limits, caching, fallback models, timeouts
- 21. Workflow and State Management in Production — Queues and asynchrony, scaling, distributed state
- 22. Prompt and Program Management — Prompt versioning, prompt regressions through evals, configs and feature flags, A/B testing
- 23. Evals in CI/CD — Quality gates in CI/CD, dataset versioning, handling flaky cases, security tests
- 24. Data and Privacy — PII detection and redaction, secret protection, log redaction, log retention and TTL
- 25. Production Readiness Index — A prioritization guide (1 day / 1–2 weeks) and quick links to production topics
Appendices¶
- Appendix: Reference Material — Glossary, checklists, SOP templates, decision tables, capability benchmark
Reading Path¶
For beginners (recommended path — practice-first)¶
- 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.” - Study The Physics of LLMs — the foundation for understanding everything else
- Master Prompting — this is the foundation of working with agents
- Build a working agent:
- Tools and Function Calling — the agent's "hands"
- Autonomy and Loops — how the agent works in a loop
- Safety and Human-in-the-Loop — protection against dangerous actions
- Extend its capabilities:
- RAG and the Knowledge Base — working with documentation
- Multi-Agent Systems — a team of specialized agents
- Evals and Reliability — testing agents
- Dive deeper into the architecture:
- Anatomy of an Agent — components and how they interact
- Planning and Workflow Patterns — planning complex tasks
- State Management — execution reliability
- Agent Memory Systems — linear memory within a run, long-term memory across sessions, prompt cache
- Context Engineering — a stable prefix, condense on overflow, avoiding over-engineering
- Practice: Work through the labs in parallel with reading the chapters
For experienced programmers¶
You can skip the basic chapters and go straight to:
- Preface → Mental Model: The Agent Is a New Employee — 5 minutes; it explains the entire security part of the course with a single idea
- Tools and Function Calling
- Autonomy and Loops
- Agent Memory Systems and Context Engineering — memory and context without over-engineering
- Case Studies — to understand real-world applications
Fast track: the core concepts in 10 minutes¶
If you are an experienced developer and want to grasp the essentials quickly:
-
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
-
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.
-
How does the agent loop work?
-
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.PromptTokensresponse, not from your own counters.
-
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 } -
What to read next:
- Chapter 03: Tools — the detailed protocol
- Chapter 04: Autonomy — the agent loop
- Chapter 09: Anatomy of an Agent — the architecture
- Chapter 12: Agent Memory and Chapter 13: Context Engineering — without over-engineering
After finishing the core course¶
After studying chapters 1–16, move on to:
- Part V: Infrastructure / Platform Security — security, governance, tool protocols
- Part VI: Production Readiness — a practical guide to production readiness with step-by-step adoption recipes
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 (
llmpackage 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¶
- Read sequentially — each chapter builds on the previous ones
- Practice in parallel — after each chapter, complete the corresponding lab
- Use it as a reference — come back to the sections you need when working on projects
- Study the examples — each chapter includes examples from different domains (DevOps, Support, Data, Security, Product)
- Do the exercises — the exercises in each chapter help reinforce the material
- Test yourself — use the checklists for self-assessment
Happy learning.