How an autonomous pentest agent decides what to attack next
Fixed playbooks break on real targets. How I design the planning loop of an autonomous pentest agent: evidence in, hypotheses out, scope enforced in code.
A vulnerability scanner runs a checklist. It sends the same requests to every target, in the same order, and reports whatever matches a signature. That's useful, but it isn't penetration testing. A human tester does something different. They look at what came back, form a guess about what might be wrong, try the cheapest way to prove or disprove it, and let the answer decide what to do next.
I spend my days building agents that do the second thing. This post is about the part that makes it work: the loop that decides what to attack next. I'll keep it general. The ideas apply to any agent built on LangGraph or something like it, not to one product in particular.
Why a fixed playbook isn't enough
The obvious way to "add AI" to pentesting is to give a model a list of steps: run recon, run a directory scan, try SQL injection on every parameter, write a report. It demos well and falls apart on real targets, for three reasons:
- Real apps don't look like the playbook. The interesting bug is usually in something specific to that app: a custom role system, an import feature, an internal API the frontend calls.
- Order matters. Finding a second user's ID changes what is worth testing. A playbook can't react to that.
- Most of the work is deciding what not to do. An agent that tries everything is slow, noisy and expensive, and it buries the one real finding under a hundred maybes.
So the job isn't "run more steps". It is "pick the next step well, every time".
The loop
The loop I keep coming back to has five steps:
- Observe. Read the results of the last action and turn them into structured evidence.
- Hypothesize. From all the evidence so far, list what might be vulnerable and why.
- Choose. Pick the one hypothesis worth testing next.
- Act. Run the tool that tests it, inside scope.
- Verify. Decide whether the result proves something, and record it either way.
Then it goes back to observe. Here's a simplified version as a LangGraph graph. This is a sketch, not production code:
from typing import TypedDict
from langgraph.graph import StateGraph, END
class Engagement(TypedDict):
scope: list[str] # hosts and paths we're allowed to touch
evidence: list[dict] # everything observed so far, with its source
hypotheses: list[dict] # what might be wrong, and why we think so
findings: list[dict] # only things we have reproduced
budget: int # actions left before we stop
graph = StateGraph(Engagement)
graph.add_node("observe", observe)
graph.add_node("hypothesize", hypothesize)
graph.add_node("act", act)
graph.add_node("verify", verify)
graph.set_entry_point("observe")
graph.add_edge("observe", "hypothesize")
graph.add_conditional_edges("hypothesize", choose_next, {"act": "act", "stop": END})
graph.add_edge("act", "verify")
graph.add_edge("verify", "observe")
agent = graph.compile()The graph itself is boring, and that's on purpose. All the intelligence sits in two places: what goes into the state, and how choose_next picks.
Evidence is the state
The most important design decision is what the agent is allowed to believe. In my agents, the model doesn't get to "remember" things. Everything it knows has to be a piece of evidence in the state, and every piece of evidence has a source: the request that was sent and the response that came back.
{
"kind": "endpoint",
"value": "GET /api/v2/users/{id}",
"observed": "id is numeric and increments by 1",
"source": "req_0142",
}That one rule fixes a lot. When the model proposes a hypothesis, it has to point at the evidence behind it. When it writes a finding, the finding links back to real traffic. If it can't cite anything, the hypothesis doesn't make it into the state. Hallucinated vulnerabilities don't survive, because there is nothing for them to stand on.
Choosing what to try next
With evidence and hypotheses in the state, choosing becomes a ranking problem. I think about each hypothesis on three axes:
- Strength of evidence. "This ID is sequential and the endpoint returns user data" is a strong lead for an access-control test. "This app uses PHP" is not.
- Cost to test. One request that would confirm or kill the idea beats a long brute-force run.
- Risk. Anything that could change data, lock accounts or put load on the target gets ranked down. If it's out of the agreed rules, it's removed entirely.
The model is good at the first axis: reading messy evidence and saying what it suggests. Code is better at the other two. So choose_next is a mix. The model scores how promising each hypothesis is, then plain code applies cost, risk and the remaining budget, and decides whether to continue or stop.
The stopping rule matters as much as the choosing rule. An agent with no budget will happily keep probing forever. Stopping when the budget is spent, or when no hypothesis clears a minimum score, keeps engagements bounded and cheap.
Verify before you report
The difference between a lead and a finding is reproduction. After an action that looks like a hit, the agent doesn't write a report. It tries to reproduce the result, ideally in a slightly different way, such as a different account, a different object or a fresh session. Only a reproduced result becomes a finding.
This is also where false positives go to die. A response that looks like a leak might just be the user's own data, a cached page or a generic error. A second, controlled request usually tells you which.
Scope lives in the tools, not the prompt
You can tell a model "only test app.example.com" in the prompt. You shouldn't rely on it. Prompts are suggestions. Tools are code.
Every tool the agent can call checks the target against the scope before it sends anything, and refuses if it doesn't match. Destructive techniques simply aren't available as tools unless the engagement allows them. The agent can be as creative as it likes in choosing what to try. It cannot choose to go somewhere it isn't allowed.
Where agents still struggle
They are not magic. In my experience the hard cases are:
- Business logic. "A refund should never exceed the original payment" is obvious to a human and invisible in the traffic unless someone tells the agent what the business is.
- Long chains. The longer the chain of steps needed to reach a bug, the more chances there are to drop the thread.
- Noisy targets. Rate limits, WAFs and flaky staging environments produce evidence that looks meaningful and isn't.
That's why I see these agents as a force multiplier for human testers, not a replacement. They cover the wide, repeatable ground and hand over evidence. People bring the judgment.
Takeaways
- Don't give the agent a playbook. Give it a loop: observe, hypothesize, choose, act, verify.
- Make evidence the only thing it can believe, and make every claim cite its source.
- Let the model judge what is promising. Let code judge cost, risk and when to stop.
- Reproduce before you report.
- Enforce scope in the tools, never only in the prompt.
If you're building something similar, or want an agent like this pointed at your own apps, get in touch.
Share this post