liveBook · Manning

Chapter 2: Building your first AI agent

What you will learn in chapter 2 of AI Agents for Offensive Security

This chapter introduces the construction of a controlled, inspectable AI agent by contrasting it with scripts and framing agent execution as a component-based decision loop.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces the construction of a controlled, inspectable AI agent by contrasting it with scripts and framing agent execution as a component-based decision loop.

Key terms: script-based automation, agents, scripts, tools, minimal agent specification, recon data triage, large language model, messages, artifacts, controlled decision-making loop

2.1 Limitations of script-based automation

This section contrasts brittle script automation and insufficient standalone LLM chatbots to motivate agents that can make accountable decisions in changing security environments.

Key terms: scripts, cron jobs, one-off tools, predefined instructions, fixed file state, artifacts, large language models (LLMs), scan output, anomalies, next steps

2.2 What is an agent?

This section defines AI agents, distinguishes them from scripts and standalone LLMs, and explains why their controlled feedback loops suit dynamic offensive-security work.

Key terms: agent, controlled decisions, perceive → reason → act → observe, feedback loop, autonomous task execution, script, procedural programming, LLM, text-prediction engine, memory

2.3 Anatomy of an agent

This section introduces an agent as an iterative structured loop connecting reasoning, action, observation, reflection, and recording.

Key terms: structured loop, reasoning, action, observation, reflection, recording

2.3.1 Core components

This section explains the five components of an AI agent and how they combine to reason over requests, use external capabilities, retain context, and produce usable outputs.

Key terms: messages, tools, memory, agent controllers, artifacts, prompt, Chain of Thought (CoT), reasoning, custom functions, API calls

2.3.2 The ReAct agent loop

This section explains the iterative ReAct framework through which an agent reasons, acts, observes results, and can reflect and record information to improve transparent decision-making.

Key terms: ReAct, Reasoning, Action, Observation, LLM, Reflection, Recording, logs, context, hallucinations

2.3.3 The scope and responsibility of an agent

This section explains that agents add observable, controlled, accountable structure around LLM reasoning for bounded security work rather than replacing existing tools or operating without limits.

Key terms: LLM, agent, messages, tools, memory, artifacts, telemetry tools, LangSmith, logs, metrics

2.4 The minimal agent specification

Defines a framework-agnostic API contract for the smallest portable, testable, and auditable agent implementation.

Key terms: minimal agent specification, API contract, messages, tool invocations, observations, artifacts, framework-agnostic, portability, surrounding workflows, auditability

2.5 Building the minimal agent

Introduces the smallest concrete agent implementation as a five-step, constrained workflow that makes agent mechanics explicit.

Key terms: memory, optimization, network access, mechanics of agency, tool, artifact, decide, execute, stop, agent model

2.5.1 What this agent does

This subsection defines a minimal local agent that extracts URLs, summarizes them, and records its ordered steps as an artifact.

Key terms: Tool A, Tool B, execution order, URL extraction, URL summarization, summary artifact, records each step, artifact, local, deterministic

2.5.2 Messages and observations

This subsection defines the conversational message roles an agent uses and the Message and Observation records needed to preserve agent activity for monitoring and tracing.

Key terms: System, User, Agent, Tool, message role, Message, Observations, role, content, timestamp

2.5.3 Tools

This subsection defines tools as an agent's interface to the world and specifies a common schema for invoking and tracing them.

Key terms: tools, custom function, API call, regex parser, vulnerability scanner, Observations class, input and output data formats, schema, agent modules, trace the movement of data

2.5.4 Tool A: extract URLs

This subsection defines a simple URL-extraction tool and clarifies its deliberately narrow, context-free responsibility.

Key terms: ExtractUrlsTool, Tool, invocation method, URL, execution context, single task

2.5.5 Tool B: summarize URLs

This subsection introduces a URL-summarization tool that transforms the prior tool’s artifact into a short structured summary while remaining independent of pipeline context.

Key terms: SummarizeUrlsTool, artifact, short summary, structured input, structured output, upstream behavior, downstream behavior

2.5.6 Artifacts and logging

This subsection defines agent outputs as artifacts and explains logging them as JSONL to create an auditable record of each run.

Key terms: artifacts, JSONL, artifact logger, run_dir, traces, model outputs, audit trail, assessments, debugging, compliance

2.5.7 The minimal agent loop

Defines the minimal agent loop as explicit tool-order decisions, actions, observations, and recorded outcomes.

Key terms: minimal agent loop, execution order, actions, observations, recorded outcomes, MinimalAgent, callbacks, implicit state, framework-managed behavior, explicit decisions

2.5.8 Putting it together

This subsection runs the minimal agent with its two tools and artifact logging, emphasizing that these simple components form the base layer for interoperable agents.

Key terms: ArtifactLogger, artifacts, minimal agent model, tools, interoperable agents, framework abstractions, orchestration, memory stores, Tool subclass, input

2.6 Safety and governance

Introduces safety and governance as technical controls that bound autonomous offensive-security agents and make them trustworthy.

Key terms: autonomy, clearly defined boundaries, offensive security automation, safety gates, sandboxing, logging, auditability, technical controls, engineering principles, trustworthy systems

2.6.1 Why safety matters

This section explains why AI offensive-security agents require strict governance, authorization, guardrails, logging, and human approval to prevent harmful, unlawful, or out-of-scope actions.

Key terms: dual-use, authorization, intent, strict governance, CFAA, automation, authorized scope, network boundaries, destructive commands, production systems

2.6.2 Building safety gates

This section defines safety gates and shows a simple gate that blocks critical targets and requires operator approval for other targets before high-risk actions proceed.

Key terms: gate, manual gate, automated gate, safety gate, critical infrastructure, operator approval, prod, payment, core-db, act() method

2.6.3 Sandboxing and isolation

This section explains why approved agent actions still need sandboxing and describes container, process, and offline-simulation isolation strategies.

Key terms: sandboxing, isolated environment, safety gates, human approval, containerization, Docker, Podman, network limits, resource quotas, filesystem restrictions

2.6.4 Comprehensive logging

This section explains how comprehensive agent logs provide replayable accountability and valuable defensive evidence.

Key terms: ArtifactLogger, schema, metadata, replay, accountability, authorized bounds, defense through transparency, SIEM rules, SOC, incident response

2.6.5 Operational policies

This section defines the operational policies that must be formalized before an agent engagement so AI-assisted security work remains authorized, privacy-conscious, accountable, and responsibly reported.

Key terms: authorized scope only, human review, approval gate, data minimization, deletion on completion, responsible disclosure, targets, time windows, prohibited actions, purge artifacts

2.6.6 Implementing kill switches

This section shows how to halt agent operations with a background global kill switch and introduces a pre-run safety checklist.

Key terms: global killswitch, active flag, emergency stop, manual abort, monitor method, keyboard input, STOP, threading library, background, defined scope

2.6.7 Safety and governance summary

This summary consolidates the safeguards that keep an automated agent reviewable, bounded, ethical, and under human control.

Key terms: safety gates, sandboxing, comprehensive logs, operational policies, kill switches, human control, automated systems, responsible agent

2.7 The triage agent

This section introduces the capstone triage agent that combines earlier agent components to rank reconnaissance findings within ethical boundaries.

Key terms: triage agent, reconnaissance data, ranked summary, ethical boundaries, messages, tools, memory, adapters, safety gates, logged

2.7.1 What is triage?

This section defines triage as using an agent after reconnaissance to reduce noisy saved results into priorities for human review.

Key terms: triage, reconnaissance, noise, attention, agent, saved output files, scan, probe, exploit, skill

2.7.2 nmap output

This subsection frames nmap reconnaissance output as noisy input that a narrowly scoped triage agent can review to identify hosts or services worth further investigation.

Key terms: nmap output, basic service discovery, unusual ports, legacy services, exposed admin interfaces, AI agent, triage, further investigation, -oX, XML

2.7.3 Agent decision scope and constraints

This section defines a triage agent as a constrained, auditable filter that reduces noisy reconnaissance results to a short list of hosts needing human attention.

Key terms: triage agent, reduce noise, surface signals, vulnerabilities, unusual exposure, services and ports, odd combinations, service pairings, group similar hosts, service stacks

2.7.4 Triage artifacts

This section defines the prioritized triage summary artifact that turns raw reconnaissance output into an explainable, actionable review list.

Key terms: prioritized triage summary, recon data, raw scan output, reasoning, triage decisions, flagged, artifact, exploit speculation, CVE matching, severity scoring

2.7.5 Practical benefits and safety constraints

This section explains how a constrained triage agent speeds recon decisions through consistent, auditable first-pass analysis without replacing human judgment or interacting with targets.

Key terms: recon, triage, raw output, first pass, low-value targets, anomalies, similar hosts, outliers, decision, flag

2.8 Summary

This summary recaps how goal-driven agents combine reasoning, controlled action, reproducible specifications, and engineered safety, illustrated by the Triage Agent.

Where this is used later

Concepts explained in this chapter

← Chapter 1: Threat-modeling agentic pipelines Chapter 3: Multi-agent pipelines and orchestration →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.