chapter two

2 Building your first AI agent

 

This chapter covers

  • Contrasting agents with script-based automation
  • Modeling agent execution as a control loop
  • Defining core agent components and boundaries
  • Implementing a minimal agent specification
  • Applying agents to recon data triage workflows

In chapter 1, we introduced AI agents and pipelines that bring reasoning, control, and accountability into offensive security workflows. We demonstrated why simple scripts and one-off LLM calls break down in dynamic environments, and why structure becomes crucial once automation starts making decisions. This chapter moves from theory to construction by building a real AI agent from the ground up.

The goal is not to build a clever demo or a production system: we illustrate how wrapping a large language model inside a small set of explicit components (messages, tools, and artifacts) turns reactive text generation into a controlled decision-making loop that can be inspected, replayed, and defended.

We’ll start by establishing a clear mental model for how agents work and by drawing firm boundaries between agents, scripts, and tools. We’ll illustrate how we can build an agent using Python without AI frameworks. After setting our foundation, we will apply frameworks, such as LangChain and AutoGen. The chapter concludes with the governance patterns that ensure agent behavior remains safe and defensible in real-world engagements.

2.1 Limitations of script-based automation

2.2 What is an agent?

2.3 Anatomy of an agent

2.3.1 Core components

2.3.2 The ReAct agent loop

2.3.3 The scope and responsibility of an agent

2.4 The minimal agent specification

2.5 Building the minimal agent

2.5.1 What this agent does

2.5.2 Messages and observations

2.5.3 Tools

2.5.4 Tool A: extract URLs

2.5.5 Tool B: summarize URLs

2.5.6 Artifacts and logging

2.5.7 The minimal agent loop

2.5.8 Putting it together

2.6 Safety and governance

2.6.1 Why safety matters

2.6.2 Building safety gates

2.6.3 Sandboxing and isolation

2.6.4 Comprehensive logging

2.6.5 Operational policies

2.6.6 Implementing kill switches

2.6.7 Safety and governance summary

2.7 The triage agent

2.7.1 What is triage?

2.7.2 nmap output

2.7.3 Agent decision scope and constraints

2.7.4 Triage artifacts