Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:0xSteph Pentest ai Observation Normalization

From Leeroopedia
Knowledge Sources
Domains LLM_Agents, Optimization, Security_Testing
Last Updated 2026-10-23 14:00 GMT

Overview

Token-cost optimization that normalizes tool output into a bounded seven-field record before feeding it to the LLM, achieving ~41% token reduction by truncating evidence and response snippets.

Description

The Observation Normalization heuristic converts whatever a probe or tool returns into a compact, structured `NormalizedResult` before exposing it via `WorkingMemory.summary_for_llm()`. The raw output is archived to the findings DB but never sent to the LLM. This keeps per-iteration token cost predictable and bounded.

The normalized record has seven fields:

  1. `tool` — handler name
  2. `target` — target URL/hostname
  3. `verdict` — confirmed, partial, failed, or inconclusive
  4. `evidence` — truncated to 120 chars
  5. `payload_used` — the payload string (or None)
  6. `http_status` — HTTP response code (or None)
  7. `response_snippet` — truncated to 200 chars (or None)

The truncation caps are the core optimization: `_EVIDENCE_MAX = 120` and `_RESPONSE_SNIPPET_MAX = 200`. CAI's published 41% token reduction (arXiv 2504.06017) comes from caps in this neighborhood.

The `summary_for_llm()` method also applies sampling caps: 10 recent findings, 30 endpoints, 20 untried (endpoint, bug-class) pairs. PentestGPT's published 228% task-completion improvement (arXiv 2308.06782) comes from this kind of structured state representation versus a flat counts dict.

Usage

Apply this heuristic when:

  • Designing new probe handlers — return a `NormalizedResult` via `Observation.normalized` so the LLM sees the compact form, not the raw output.
  • Tuning token budget — adjust `_EVIDENCE_MAX` and `_RESPONSE_SNIPPET_MAX` in `engine/agent_loop.py` to control per-iteration token cost.
  • Debugging LLM context — inspect `memory.summary_for_llm()` to see exactly what the LLM receives; raw output is in `Observation.raw` (archived to DB only).
  • Working with legacy probes — probes built before normalization leave `normalized=None`; the legacy fallback in `working_memory` handles those.

The Insight (Rule of Thumb)

  • Action: Normalize all tool output to a bounded seven-field record before exposing it to the LLM. Archive raw output separately.
  • Value: `_EVIDENCE_MAX = 120`, `_RESPONSE_SNIPPET_MAX = 200`. Sampling caps: 10 findings, 30 endpoints, 20 untried pairs.
  • Trade-off: Truncation loses detail — the LLM sees a summary, not the full response. The raw output is preserved in the findings DB for post-hoc analysis, but the LLM cannot reason over it in-flight.
  • Compatibility: Legacy probes with `normalized=None` are handled by a fallback in `working_memory`; the normalization layer is backward-compatible.

Reasoning

Every byte fed back into the LLM costs tokens. Raw tool output (HTTP responses, scan results, evidence text) can be thousands of characters — far more than the LLM needs to decide the next action. The LLM only needs: what happened (verdict), what was tried (payload), what the target said (status + snippet), and the key evidence (truncated).

The 120/200 char caps are tuned to preserve the decision-relevant information while cutting the noise. An HTTP response may be 10KB, but the LLM only needs to know the status code and whether the payload was reflected — 200 chars of response snippet covers that.

The sampling caps in `summary_for_llm()` (10/30/20) serve a similar purpose: the LLM doesn't need to see all 200 endpoints discovered, just a representative sample plus the untried pairs it should consider next. This structured state representation is what drives the published task-completion improvements.

Code Evidence

Truncation caps from `engine/agent_loop.py:57-62`:

# Truncation caps for normalized observation fields. Keeping these
# small is the whole point of normalization, every byte we feed back
# into the LLM costs tokens. CAI's published 41% reduction comes
# from caps in this neighborhood (arXiv 2504.06017).
_EVIDENCE_MAX = 120
_RESPONSE_SNIPPET_MAX = 200

NormalizedResult fields from `engine/agent_loop.py:82-110`:

@dataclass
class NormalizedResult:
    tool: str
    target: str
    verdict: Verdict
    evidence: str
    payload_used: str | None = None
    http_status: int | None = None
    response_snippet: str | None = None

    def __post_init__(self) -> None:
        if self.evidence and len(self.evidence) > _EVIDENCE_MAX:
            self.evidence = self.evidence[:_EVIDENCE_MAX]
        if (
            self.response_snippet is not None
            and len(self.response_snippet) > _RESPONSE_SNIPPET_MAX
        ):
            self.response_snippet = self.response_snippet[:_RESPONSE_SNIPPET_MAX]

Sampling caps in summary_for_llm from `engine/working_memory.py:228-229`:

# Sampling caps: 10 recent findings, 30 endpoints, 20 untried pairs.
# PentestGPT's published 228% task-completion improvement (arXiv 2308.06782)

Raw output never sent to LLM from `engine/agent_loop.py:126-133`:

@dataclass
class Observation:
    """The result of an action. Goes back into working memory.
    `raw` is archived to the findings DB but NEVER sent to the LLM.
    `normalized` is the compact shape the agent loop exposes via
    WorkingMemory.summary_for_llm.
    """

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment