Plenty of people who say “I have an AI agent” have a chat window with a long prompt pasted in. That is not an agent, and the difference matters more than it looks once you give it access to your code.
In this post we look at what a real agent is and build a small, useful one: a security reviewer that reads your code, finds problems, and cannot touch anything. Concept first, since it works in any tool. Then how to set it up in Claude Code and in Codex CLI.
What an agent is and what it is not
An agent has four pieces:
Model. The part that reasons and picks the next step.
Instructions. Who it is, what it has to achieve, and where it stops.
Tools. What it can do besides generating text: read files, search the repo, run commands.
Loop. It tries something, looks at the result, and decides whether to continue or finish.
What separates it from a chat is the loop and the tools. In a chat, you copy the code, paste it, read the answer, and decide. With an agent, the system opens the files it needs, searches for patterns, checks what it found, and keeps going until it is done. You get the report.
That makes it more useful and also more dangerous, because the model now acts on your environment. So a big part of designing a good agent is deciding what it cannot do.
For the background theory, the AI series covers specialized agents and the harness that runs them. Here we go straight to building.
Anatomy of a good agent
A security reviewer is a good first agent because the risk is low: it only needs to read. These are the five design decisions that matter, and none of them depends on your tool.
1. A specific role. “You are a web application security reviewer” works. “You are an expert in everything” does not. The role sets the lens it uses on the code.
2. A narrow scope. Which categories it checks: injection, hardcoded secrets, input validation, authentication and authorization. A short list gives it focus. Without one, you get a general opinion about style.
3. Read-only tools. Read, search by content, search by filename. No editing, no running commands. A reviewer that cannot write cannot break anything, whether by mistake or because of a malicious instruction hidden in the code it is reading.
4. A fixed output format. Every finding with file and line, severity, what is wrong, and how to fix it. When the format never changes, you can compare reviews and discard noise fast.
5. A list of what it does NOT do. It does not modify files, it does not invent vulnerabilities it cannot point to on a specific line, and it does not claim the code “is secure”. That last rule matters most to me: an automated reviewer finds problems, it does not certify their absence.
Setting it up in Claude Code
If you do not have it yet, the Claude Code quickstart recommends this install on macOS, Linux, or WSL:
curl -fsSL https://claude.ai/install.sh | bash
According to the subagents documentation, a subagent is a Markdown file with YAML frontmatter. The frontmatter is the configuration and the body is the system prompt. Only name and description are required. You store it in .claude/agents/ for one project or ~/.claude/agents/ for all your projects.
Two optional fields are worth knowing from day one. tools is the list of tools it may use, and if you omit it the subagent inherits every tool available; that is why the list is explicit here. model picks the model (for example sonnet, opus, haiku, or inherit); leave it out and Claude Code chooses one for you.
Create .claude/agents/security-reviewer.md:
---
name: security-reviewer
description: Reviews code for security vulnerabilities (SQL injection, hardcoded secrets, missing input validation, authentication flaws). Use when an endpoint, a database query, or code that handles credentials is added or changed. Read-only, never modifies anything.
tools: Read, Grep, Glob
---
You are an application security reviewer. Your job is to find
vulnerabilities in the code you are pointed at and report them with evidence.
## Scope
Review only these categories:
1. Injection (SQL, command, template).
2. Hardcoded secrets (keys, tokens, passwords, connection strings).
3. Missing or insufficient input validation.
4. Missing or misapplied authentication and authorization.
Review only the files or folders you are given. If none are given, ask
for a specific path instead of walking the whole repository.
## How you work
- Use Grep and Glob to locate entry points and queries.
- Use Read to confirm context before reporting.
- Report only what you can point to with a file and a line.
## Output format
For each finding:
- Severity: CRITICAL, HIGH, MEDIUM or LOW
- Location: file:line
- Problem: one or two sentences
- Fix: a short code example
End with a one-line summary: how many findings per severity.
If you find nothing, say so and state which categories you checked.
## What you do NOT do
- You do not modify or create files.
- You do not run commands.
- You do not invent findings to look useful.
- You do not declare the code "secure". You only report what you found.
Look at the description. Claude uses it to decide when to delegate to this agent, so it has to say when to use it, not just what it is.
Claude Code watches that folder, so you do not need to restart for it to pick up the new file (if it cannot find it, the docs suggest restarting). To use it, name it in your message: “Use the security-reviewer subagent to review routes/users.js”. You can also type @ and pick it from the list. The subagent works in its own context, separate from your conversation, and hands you back the report.
Setting it up in Codex CLI
The Codex GitHub page gives two ways to install it, and then you start it with codex:
npm install -g @openai/codex
# or with Homebrew
brew install --cask codex
Before creating the agent, one distinction that trips people up. Codex reads AGENTS.md files (in ~/.codex/, at the repo root, and in each folder down to where you are working) as standing instructions for the main session. That is not a separate agent; it is context the main agent always gets. What we want here is a custom agent with its own role.
The Codex subagents documentation describes custom agents as standalone TOML files, one per agent, in .codex/agents/ for a project or ~/.codex/agents/ for personal use. The required fields are name, description, and developer_instructions, and Codex identifies the agent by name, not by filename.
The file .codex/agents/security-reviewer.toml looks like this:
name = "security-reviewer"
description = "Reviews code for vulnerabilities (SQL injection, hardcoded secrets, input validation, authentication). Read-only, never modifies anything."
developer_instructions = """
You are an application security reviewer.
Check only injection, hardcoded secrets, input validation, and
authentication/authorization, in the files you are pointed at.
Do not modify or create files.
For each finding: severity (CRITICAL/HIGH/MEDIUM/LOW), file:line,
problem, and fix. Never declare the code secure.
"""
Here is the real difference between the two tools. According to that documentation, the agent file cannot set sandbox_mode or approval_policy: the subagent inherits the sandbox and approval settings of the session that launches it. Writing “do not modify files” in the instructions does not create a read-only sandbox. In Claude Code, tools: Read, Grep, Glob does limit what the agent can use. In Codex, you set the boundary when you start the session:
codex --sandbox read-only --ask-for-approval never
With read-only the session can read files and run commands inside a read-only sandbox, and with never Codex never asks you for approval to step outside it. If you want it fixed, the approvals and security page shows the config.toml equivalent (sandbox_mode = "read-only"), and inside a session you can switch with /permissions.
To use it, ask directly: “Use the security-reviewer agent to review routes/users.js”. According to the docs, current local Codex releases spawn subagents after a direct request or when a project or skill instruction calls for it.
To me this difference is the most useful lesson in the post: instructions are a request, tools and sandbox are a boundary.
Trying it on vulnerable code
This endpoint packs three classic problems into a few lines:
// routes/users.js
const API_KEY = "sk_live_EXAMPLE_NOT_A_REAL_KEY";
app.get("/users", async (req, res) => {
const { name } = req.query;
const rows = await db.query(
`SELECT id, email FROM users WHERE name = '${name}'`
);
res.json(rows);
});
app.post("/users", async (req, res) => {
const { email, age } = req.body;
await db.query("INSERT INTO users (email, age) VALUES ($1, $2)", [email, age]);
res.sendStatus(201);
});
You ask it to review routes/users.js. With the format we gave it, the expected output looks like this:
1. Severity: CRITICAL
Location: routes/users.js:7
Problem: the query concatenates `name`, which comes from the query
string, straight into the SQL. This allows SQL injection.
Fix: db.query("SELECT id, email FROM users WHERE name = $1", [name])
2. Severity: HIGH
Location: routes/users.js:2
Problem: hardcoded API key in source. It will live in git history.
Fix: const API_KEY = process.env.API_KEY; and rotate the key.
3. Severity: MEDIUM
Location: routes/users.js:14
Problem: `email` and `age` are used without checking type, format or
range. The query is parameterized, but invalid data still reaches the database.
Fix: validate with a schema (for example zod) before inserting.
Summary: 1 critical, 1 high, 1 medium, 0 low.
“Expected” means this is what the design should produce, not a pasted run. The exact text varies between runs and between models, and I did not measure how consistent it is.
Beginner mistakes
Giving it write permissions “just in case”. A reviewer that can edit stops being a reviewer. And if the code it reads contains text written to trick the AI, an agent with write access is the one that might obey it. Read-only, always.
A vague description. “Helps with security” does not tell the tool when to use it. Say when: “when adding or changing endpoints, queries, or credential handling”.
Telling it “review everything”. With no scope, the agent wanders or burns your token budget reading irrelevant files. Give it a folder or a list of files.
Trusting without verifying. Picture the agent flagging a password123 in a test file under tests/fixtures as CRITICAL. That is a false positive, because that value opens nothing. The agent was right about the pattern and wrong about the context. That is why every finding carries a file and line: so you open it and decide. An agent like this saves you search time, not judgment.
What to take with you
An agent is a model plus instructions, tools, and a loop, and what makes a good one is its limits: a clear role, a short scope, read-only access, a fixed format, and a list of what it does not do. The file format changes between tools; the design does not.
The next step
This reviewer runs when you ask it to. If you want it to check every commit without you remembering, a security agent that reviews your code before every commit takes this same idea into a pre-commit hook that blocks the commit when it finds secrets or SQL injection, with a video and the full files.
Meanwhile, set up your security-reviewer, run it on one of your own endpoints, and tell me what it found by DM on Instagram or in the Discord community. If it gave you a false positive, even better: I want to see it.
