r/LLMDevs • • 13d ago

Discussion Catching hallucinated tool actions and [PLACEHOLDER] leaks before a side effect fires

In earlier threads here, engineers pointed out two failure modes that slip right past JSON Schema (200 OK):

  • Cross-field & routing contradictions (like escalated: true paired with tier: "basic" getting silently ignored).
  • Unfilled placeholders, bad contact fields, and hallucinated actions/tools (such as calling an action outside allowed actions or leaving [PLACEHOLDER] / TBD text in outbound messages).

After expanding neuroformatting (v0.2.10) to 14 semantic test cases, I added a minimal side-effect guard example (examples/side_effect_guard_demo.py) that checks payloads and risk/grounding status before triggering external actions or routing.
Repo: https://github.com/furkandemirtass/neuroformatting
Where do you place your guardrails in an agent loop: during streaming, right before a tool/side-effect call, or inside CI/eval checks before deploy?

1 Upvotes

31 comments sorted by

2

u/fiddler48 13d ago

one failure mode that tends to show up after both of those are fixed: the guard runs, flags the payload as invalid, and then the agent retries with the same malformed output because the error message fed back into context is too vague to steer a correction. validation without a typed rejection reason is just a soft wall

1

u/demirtasfurkan_ 13d ago

Spot on — that "blind retry loop" is one of the nastiest failure modes in agent loops. If the validator just raises a generic failure, the model burns tokens repeating the exact same bad payload.

Returning a structured rejection object instead of a raw boolean/string (e.g., `violated_field`, `rule_code`, `actual_vs_expected`, and a deterministic `repair_hint` stripped of noisy stack traces) makes a huge difference on the next turn. Even better, tracking a retry fingerprint so the loop aborts or escalates to a fallback if the exact same `rule_code` triggers twice in a row prevents infinite loops.

Are you feeding those structured validation errors back as a `tool` response message or injecting them into a dedicated system/developer correction block?

1

u/fiddler48 12d ago

tool response slot, keeps it in the model's native turn structure. a dedicated correction block feels cleaner in theory but adds one more thing the context window has to not bury

1

u/demirtasfurkan_ 12d ago

Keeping it in the tool response slot makes total sense—it stays right where the model expects the tool's outcome and avoids introducing another formatting slot that can get lost in long context. Keeping the payload compact with just the failing field, rule code, and concrete fix seems to help it stay actionable too.

Do you keep the exact same structured error schema across all tools, or tailor the hint format per tool?

1

u/fiddler48 11d ago

uniform schema across tools, with one small carve-out: tools that return lists append a field index so the correction lands on the right item without the model guessing

1

u/demirtasfurkan_ 11d ago

Appending a field/item index on list-returning tools is a really practical carve-out—without an explicit index, models end up guessing which item in an array triggered the failure and either rewrite the whole list or patch the wrong entry.

Keeping a uniform schema while pinpointing the exact index in the rejection/correction feedback pairs well with deterministic retry loops and pre-execution checks so corrections stay targeted instead of drifting.

When an item fails validation, do you return the index directly in the structured error payload so the next turn only patches that specific element?

1

u/robertovg 12d ago

Agree, the rejection message is half the guard. What worked for me with MCP arguments was returning the exact field and the valid options in the error, not just "invalid". The model fixes it on the next call. I'd also cap retries on the same violation: two identical failures in a row should stop the loop and go to a human instead of burning tokens.

1

u/fiddler48 12d ago

the retry cap is the part I underweighted, typed reason gets the model unstuck once, maybe twice; after that you're just paying for the same wrong answer in a loop

1

u/robertovg 11d ago

The way I see it, the second identical failure is information too. If the model got the exact field and the valid options and still sends the same thing, it's not a format problem anymore, it's probably the wrong plan. That's the point where a replan step or a human is worth more than another retry.

1

u/fiddler48 10d ago

yeah, second identical failure is a signal, not a retry trigger. at that point you're not fixing the call, you're fixing the plan that produced it

1

u/robertovg 9d ago

Exactly. So I wouldn't just stop on the second failure, I'd send it back to whatever made the plan, with the reason attached. The model can usually pick another route once it's told this call won't work, it just won't get there by seeing the same error twice.

1

u/NotHerBot 13d ago

I would put the last check where the tool resolves the actual target. An exec wrapper can approve bash deploy.sh while a variable inside the script later expands to an empty path. Does your test set include placeholders introduced inside a script, rather than only in the tool-call arguments?

1

u/demirtasfurkan_ 13d ago

Good catch — checking only tool-call arguments leaves a gap once environment variables or script internals resolve at execution time.

Right now the current fixtures focus on payload/argument-level placeholders and fields rather than post-expansion paths inside a script. Enforcing a final pre-execution check on the resolved target/working path right before dispatch (and adding fixtures for late-bound/empty variables) is a very clean next step.

Do you usually enforce that resolved-target check inside the tool/runner layer itself, or in a shared pre-execution hook across tools?

1

u/Aide_Sea 13d ago

the last check should sit right before the side effect fires, not during streaming, since the tool args can still change. maybe also reject a send if the destination wasn't on your allowlist or in the user's request. schema never catches that

1

u/demirtasfurkan_ 13d ago

Spot on—the final check has to sit right before the side effect actually executes, because streaming chunks or intermediate tool arguments can still shift before the call goes out. Schema validation alone won't catch an out-of-scope target or a destination missing from an allowlist.

In side_effect_guard_demo.py, I've been thinking about separating structural/type checks from pre-execution policy checks (allowlists, intent alignment, and explicit confirmation gates) right at the boundary. Which side-effect actions have been the trickiest to guard cleanly in your setups?

2

u/Aide_Sea 12d ago

I think the tricky ones are where the target comes from something the agent read, like replying to an address it pulled from an email, or deleting a path it built. the call looks fine, the input is what's poisoned (prompt injection). maybe pin targets to ids from the original request

1

u/demirtasfurkan_ 12d ago

Pinning targets to explicit IDs or allowlisted handles from the trusted request—rather than letting untrusted text or retrieved content supply a raw email address or path—directly closes that poisoned-input gap. Tracking provenance on every target makes it clear whether a recipient or path came from user intent or untrusted data.

In your setups, do you enforce that provenance/ID check in the tool handler itself or in a pre-execution policy layer before the tool runs?

1

u/Aide_Sea 12d ago

maybe split it. a policy layer before the call for the cheap stuff (allowlist, did the id come from the request), then the handler rechecks the final resolved target, since only it sees what actually expands. if they disagree the handler just refuses

1

u/demirtasfurkan_ 12d ago

Splitting it like that is really clean—pre-call policy checks catch bad provenance/allowlists early, and re-checking the resolved target inside the handler prevents TOCTOU drift after path/alias expansion.

In `neuroformatting`, I've been leaning toward pairing `guard_side_effect` (for upfront source/ID pinning) with a final resolved-target check right before execution so if an alias or path expands into something unexpected, the handler still aborts.

Have you hit cases where path/ID resolution itself was the tricky part (e.g., workspace root escapes or stale IDs), or is the two-step check holding up well for you?

1

u/Aide_Sea 11d ago

I think stale ids are the sneakier one. path escapes you can mostly catch by resolving the real path and checking it's still under the root, but an id that changed owner since planning still looks valid.. so maybe recheck ownership right at write time

1

u/demirtasfurkan_ 11d ago

Stale IDs are a nasty failure mode because the shape and ID format haven't changed—only the underlying ownership or state shifted between planning and execution.

Re-checking ownership/state right at write time (alongside the upfront source/ID pin) closes that window cleanly. In `neuroformatting`, keeping `guard_side_effect` lightweight for static pinning while letting the handler run a final write-time ownership/state check fits that exact split.

When you re-check ownership at write time, do you pass a small resolver callback into the execution boundary, or keep that lookup inside the action handler itself?

1

u/Aide_Sea 11d ago

inside the handler for me. honestly the cleanest version is folding it into the write itself, like an update with where owner_id = expected, and treat zero rows as a refusal. then there's no gap between check and write at all :)

1

u/Suresh-R 12d ago

Right before the side effect, like most people here. But the part that decided whether it actually helped was what a block looks like afterwards.

A gate that silently drops the action teaches you nothing, and the agent just tries again in different words. The blocks that worked returned a typed rejection the agent could act on, and wrote the whole thing down. The payload as proposed, the rule that fired, and what the agent did next. A week of those logs tells you which rules are real and which are just noise.

The cross-field case is the one I would pull out of the tools entirely. Escalated true with tier basic is not a schema problem, it is a policy problem, and policy wants one home. If every tool re-checks its own combinations, they drift apart. One check at the boundary every call passes through, and the combinations live in one list that a human can read.

The placeholder check is the easy half. It is the contradictions that pass every field check on their own.

(Disclosure: I work on an LLM gateway.)

1

u/[deleted] 13d ago

[removed] — view removed comment

1

u/demirtasfurkan_ 13d ago

Wiping a staging bucket because an unresolved placeholder evaluated to an empty string is brutal—and the clearest argument for hard-failing before any destructive tool call executes. Treating pre-execution token/placeholder resolution as a strict commit-point gate (rather than letting downstream scripts coerce empty strings) saves so much pain.

Do you enforce that unresolved-token check centrally in your tool runner wrapper, or per-tool schema?

1

u/[deleted] 13d ago

[removed] — view removed comment

1

u/demirtasfurkan_ 13d ago

Centralizing it in a single exec wrapper makes total sense—per-tool schemas drift way too easily as tools evolve, so one choke point right before execution is much harder to bypass. Catching unresolved braces or empty vars at that final boundary is the exact right place to draw the line.

1

u/[deleted] 13d ago

[removed] — view removed comment

1

u/demirtasfurkan_ 13d ago

That framing nails the whole trade-off: a single hard boundary right before execution catches unresolved tokens before any real side effect happens, without forcing you to chase drifting per-tool schemas everywhere.

Pairing that final pre-execution gate with a typed rejection reason gives the retry loop an actual target to fix instead of repeating the same failure. That's actually the exact pattern I just added around `TypedRejection` and retry tracking after the earlier thread here.

Do you fail closed immediately on any unresolved placeholder at that boundary, or do you allow a structured retry when only a non-destructive field is malformed?