Inferensys

Prompt

Stale State Detection and Correction Prompt Template

A practical prompt playbook for using Stale State Detection and Correction Prompt Template in production AI workflows.
Operations team reviewing AI workflow automation on laptop, workflow builder visible, casual office setup.
PROMPT PLAYBOOK

When to Use This Prompt

Define the job, reader, and constraints for detecting and correcting stale agent state.

This prompt is for agent runtimes that maintain an internal representation of the world—task progress, tool outputs, user preferences, or environmental facts—and need to detect when that representation has diverged from ground truth. The job-to-be-done is comparing a believed state (what the agent thinks is true) against an observed state (fresh evidence from tools, user input, or environment checks) and producing a structured diff with correction actions. The ideal user is an engineering team building autonomous or semi-autonomous agents that operate over multiple turns, where stale state causes repeated work, dropped subtasks, or confident but incorrect decisions.

Use this prompt when your agent has a stored state object that persists across tool calls or conversation turns, and you have a mechanism to re-observe ground truth—such as re-reading a file, re-querying an API, or asking the user for confirmation. The prompt is designed for reconciliation loops: periodic checks, post-tool-failure recovery, session resumption, and handoff validation. It works best when the believed state and observed state share a common schema, making diff generation straightforward. Wire this into a state management module that triggers the prompt on a schedule, after tool errors, or when confidence scores drop below a threshold.

Do not use this prompt for initial state construction from raw observations—that belongs to extraction and normalization prompts. Avoid it when the state is purely conversational and has no external ground truth to check against. This prompt is also inappropriate for real-time control systems where latency constraints prevent a full diff cycle. For high-risk domains where incorrect state could trigger irreversible actions (financial transactions, clinical decisions, infrastructure changes), always route the correction proposal through a human approval step before applying changes. The output diff should be logged as an audit event with the evidence that triggered the correction.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Stale State Detection and Correction prompt template delivers value and where it introduces risk. Use these cards to decide whether this prompt fits your agent architecture before wiring it into a production loop.

01

Good Fit: Long-Running Autonomous Agents

Use when: your agent operates across many turns or tool calls where internal beliefs drift from external reality. Guardrail: schedule state reconciliation at predefined checkpoints, not after every step, to balance correctness with token cost.

02

Bad Fit: Stateless Single-Turn Requests

Avoid when: the agent has no persistent state between calls or the ground truth is fully contained in the immediate context window. Guardrail: skip the detection prompt entirely; adding state reconciliation to stateless flows creates false-positive drift signals and wastes inference budget.

03

Required Inputs

Must provide: the agent's current believed state, a fresh observation or ground-truth snapshot, and the set of fields considered authoritative. Guardrail: validate that observation data is fresher than the believed state timestamp before running the prompt; stale observations produce correction loops.

04

Operational Risk: Confirmation Bias

What to watch: the model may treat its believed state as presumptively correct and dismiss contradictory observations as noise. Guardrail: instruct the prompt to assume observations are authoritative unless explicitly flagged as low-confidence; include a mandatory conflict-resolution field in the output schema.

05

Operational Risk: Over-Correction Cascades

What to watch: a single stale field triggers a correction that invalidates dependent state, causing a chain of unnecessary rewrites. Guardrail: limit the correction scope to directly observed discrepancies; require a separate replanning step for downstream state changes rather than bundling them into the detection prompt.

06

Operational Risk: Silent State Corruption

What to watch: the prompt reports no drift when the observation source itself is incomplete or the diff logic misses partial mismatches. Guardrail: include a state integrity validation step before the diff; if observation completeness is below threshold, flag the reconciliation as inconclusive rather than clean.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable prompt for detecting when an agent's internal state diverges from observed ground truth and generating a structured correction plan.

The prompt below is designed to be dropped into an agent runtime whenever a state snapshot must be validated against fresh observations. It forces the model to produce a structured diff—not a narrative summary—so that downstream code can apply corrections deterministically. Use it after tool calls that return new data, before committing to irreversible actions, or when a checkpoint is loaded and must be reconciled with the current environment.

text
You are an agent state auditor. Your task is to compare the agent's BELIEVED STATE with the OBSERVED STATE and produce a structured correction plan.

BELIEVED STATE:
[BELIEVED_STATE]

OBSERVED STATE:
[OBSERVED_STATE]

STATE SCHEMA:
[STATE_SCHEMA]

CONFIDENCE THRESHOLD: [CONFIDENCE_THRESHOLD]

INSTRUCTIONS:
1. Identify every field in the BELIEVED STATE that does not match the OBSERVED STATE.
2. For each mismatch, classify it as one of: STALE (outdated value), MISSING (present in observed but absent in believed), EXTRANEOUS (present in believed but absent in observed), or CONTRADICTORY (both present but conflicting).
3. For each mismatch, propose a CORRECTION ACTION: UPDATE, DELETE, INSERT, or ESCALATE.
4. Assign a CONFIDENCE score (0.0 to 1.0) to each mismatch based on how certain you are that the observed state is correct and the believed state is wrong.
5. If any mismatch has confidence below [CONFIDENCE_THRESHOLD], flag it for HUMAN REVIEW with a specific question that needs resolution.
6. Do NOT propose corrections for fields where the believed and observed values are equivalent after normalization (e.g., "in_progress" vs "IN_PROGRESS", null vs absent).
7. If no mismatches are found, return an empty corrections array with status "CONSISTENT".

OUTPUT SCHEMA:
{
  "status": "CONSISTENT" | "DRIFT_DETECTED" | "CONFLICT_REQUIRES_REVIEW",
  "corrections": [
    {
      "field_path": "string (dot-notation path to the mismatched field)",
      "mismatch_type": "STALE | MISSING | EXTRANEOUS | CONTRADICTORY",
      "believed_value": "any (the value in believed state, or null if missing)",
      "observed_value": "any (the value in observed state, or null if missing)",
      "correction_action": "UPDATE | DELETE | INSERT | ESCALATE",
      "proposed_value": "any (the corrected value to apply, or null for DELETE)",
      "confidence": "float between 0.0 and 1.0",
      "requires_human_review": "boolean",
      "review_question": "string (only if requires_human_review is true, else null)",
      "rationale": "string (brief explanation of why this mismatch was flagged)"
    }
  ],
  "summary": "string (one-sentence summary of findings)"
}

CONSTRAINTS:
- Do not hallucinate observed values. Only use data present in OBSERVED STATE.
- Do not flag cosmetic differences (whitespace, casing, equivalent null representations) as mismatches.
- If the OBSERVED STATE is incomplete or ambiguous, set confidence low and escalate rather than guessing.
- The output must be valid JSON conforming exactly to the OUTPUT SCHEMA.

Adaptation guidance: Replace [BELIEVED_STATE] with the agent's current internal state object, typically serialized as JSON. [OBSERVED_STATE] should be the fresh data returned by a tool call, environment probe, or user correction. [STATE_SCHEMA] is a JSON Schema or TypeScript interface describing the expected shape of both states—this helps the model normalize field names and types before comparing. [CONFIDENCE_THRESHOLD] is a float (we recommend 0.7 as a starting point) below which mismatches are escalated for human review rather than auto-applied. If your agent runtime cannot safely apply corrections automatically, set the threshold to 1.0 to force human review on every mismatch.

What to do next: Wire this prompt into your agent's state reconciliation loop. After every tool call that mutates external state, capture the result as [OBSERVED_STATE] and run this prompt before the agent acts on its believed state. Log every correction object—especially those with requires_human_review: true—to your audit trail. If you see frequent false-positive mismatches, tighten the normalization rules in instruction 6 or add a pre-processing step that canonicalizes both states before comparison. Avoid running this prompt on every turn if state is large and changes are rare; instead, trigger it on checkpoint load, tool failure recovery, and before high-cost actions.

IMPLEMENTATION TABLE

Prompt Variables

Required and optional inputs for the Stale State Detection and Correction prompt. Each placeholder must be populated before the prompt is assembled and sent to the model.

PlaceholderPurposeExampleValidation Notes

[BELIEVED_STATE]

The agent's current internal representation of task progress, completed steps, and known facts

{"task_id": "t-42", "completed": ["step-1", "step-2"], "current_step": "step-3", "known_facts": {"user_email": "a@b.com"}}

Must be valid JSON. Schema check: require task_id, completed array, and known_facts object at minimum. Reject if empty or malformed.

[OBSERVED_EVIDENCE]

Fresh ground-truth observations from tools, APIs, or user input that may contradict the believed state

{"tool": "email_lookup", "result": {"user_email": "x@y.com"}, "timestamp": "2025-01-15T10:30:00Z"}

Must be valid JSON with tool name, result payload, and timestamp. Null allowed if no new observations exist. Timestamp required for staleness comparison.

[STATE_SCHEMA]

The expected schema that both believed and observed state should conform to

{"type": "object", "required": ["task_id", "completed", "current_step", "known_facts"], "properties": {...}}

Must be valid JSON Schema draft. Use for structural validation before diff generation. Reject prompt assembly if schema is missing.

[CONFIDENCE_THRESHOLD]

Minimum confidence score (0.0-1.0) required before auto-applying a correction without human review

0.85

Must be a float between 0.0 and 1.0. Values below 0.7 should trigger mandatory human review flag. Default to 0.8 if not specified.

[MAX_CORRECTIONS]

Upper bound on the number of state corrections the model may propose in a single response

5

Must be a positive integer. Prevents over-correction spirals. Recommended range: 3-10. Reject if 0 or negative.

[CORRECTION_RULES]

Domain-specific constraints on what types of corrections are allowed or forbidden

["never_overwrite_user_confirmed_facts", "require_citation_for_contradictions", "preserve_timestamps_on_no_change"]

Must be a non-empty array of rule strings. Each rule must map to a known validator in the harness. Reject if empty or contains unrecognized rules.

[PREVIOUS_CORRECTIONS]

Log of recent corrections applied to this state to detect oscillation or over-correction patterns

[{"field": "user_email", "from": "a@b.com", "to": "x@y.com", "timestamp": "2025-01-15T10:29:00Z"}]

Must be a JSON array. Null allowed on first run. If present, check for repeated toggling of same field within last 3 corrections as oscillation signal.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the stale state detection prompt into an agent runtime with validation, retries, and safe correction application.

The stale state detection prompt is not a standalone utility; it is a state-repair module that sits between the agent's belief state and ground-truth observations. In a production harness, this prompt fires when a discrepancy trigger activates: a tool returns unexpected data, a precondition check fails, a human provides a correction, or a scheduled reconciliation interval elapses. The harness must supply two structured inputs: [BELIEVED_STATE] (the agent's current internal representation) and [OBSERVED_STATE] (fresh evidence from tools, sensors, or human input). Both inputs should share a common schema so the model can produce a meaningful diff rather than a vague narrative. If the schemas differ, add a pre-processing step that normalizes them before calling the prompt.

Validation and retry logic is critical because stale-state corrections can corrupt state if applied incorrectly. After the model returns a diff with correction_actions, run a structural validator that checks: (1) every correction_actions entry references a valid field path in the state schema, (2) confidence scores are present and within 0.0–1.0, (3) evidence_source fields point to actual observation records, and (4) no correction contradicts another correction in the same batch. If validation fails, retry once with the validation errors appended to [CONSTRAINTS]. If the second attempt also fails, escalate to a human reviewer with the raw diff and validation report. For high-risk domains (healthcare, finance, safety-critical operations), require human approval before applying any correction with confidence below 0.85 or any correction that modifies a safety-relevant field.

Model choice and latency budget shape how you deploy this prompt. For low-latency agent loops where state drift detection runs inline before every action, use a fast model (Claude 3 Haiku, GPT-4o-mini) with a strict 2-second timeout and skip the prompt entirely if the state hasn't changed since the last check. For periodic deep reconciliation (every N steps or on session resume), use a more capable model (Claude 3.5 Sonnet, GPT-4o) and allow up to 10 seconds. Log every invocation with the believed state, observed state, raw diff output, validation result, and whether corrections were applied or escalated. This audit trail is essential for debugging agent loops where the same stale state keeps reappearing. Wire the correction application step as an idempotent state merge: apply only corrections that passed validation, record a version increment on the state object, and retain the pre-correction snapshot for rollback if downstream steps fail after the repair.

IMPLEMENTATION TABLE

Expected Output Contract

Schema and validation rules for the stale-state detection output. Use this contract to parse, validate, and route the model response before applying corrections.

Field or ElementType or FormatRequiredValidation Rule

state_diff

Array of objects

Must be a non-empty array. Each element must contain 'field_path', 'believed_value', 'observed_value', and 'discrepancy_type' keys.

state_diff[].field_path

String (dot-notation)

Must match the pattern ^[a-zA-Z_][a-zA-Z0-9_](.[a-zA-Z_][a-zA-Z0-9_])*$. Reject if path does not exist in the believed-state schema.

state_diff[].believed_value

Any valid JSON

Must be present. Use null explicitly when the agent believed the field was absent. Type must match the expected schema for the field_path.

state_diff[].observed_value

Any valid JSON

Must be present. Use null explicitly when observation shows the field is absent. Type must match the expected schema for the field_path.

state_diff[].discrepancy_type

Enum string

Must be one of: 'stale_value', 'missing_field', 'extraneous_field', 'type_mismatch', 'constraint_violation'. Reject any other value.

correction_actions

Array of objects

Must be a non-empty array. Each element must contain 'action', 'target_field', 'proposed_value', and 'confidence' keys. Array length must equal state_diff length.

correction_actions[].action

Enum string

Must be one of: 'update', 'delete', 'insert', 'no_op'. 'no_op' is valid only when discrepancy_type is absent and confidence is above 0.9.

correction_actions[].target_field

String (dot-notation)

Must exactly match a field_path from the corresponding state_diff entry. Reject if no matching field_path exists.

correction_actions[].proposed_value

Any valid JSON

Must be present. For 'delete' action, must be null. For 'update' or 'insert', must match the observed_value from the corresponding diff entry.

correction_actions[].confidence

Number (0.0 to 1.0)

Must be a float between 0.0 and 1.0 inclusive. Values below 0.5 require human review before applying. Values below 0.3 must be escalated.

correction_actions[].evidence_source

String or null

If present, must be a non-empty string describing where the observed_value was obtained. Required when confidence is below 0.8. Null allowed otherwise.

escalation_required

Boolean

Must be true if any correction_actions[].confidence is below 0.3, or if state_diff contains more than 5 entries, or if any discrepancy_type is 'constraint_violation'. Otherwise false.

escalation_reason

String or null

Required when escalation_required is true. Must be a non-empty string summarizing the reason. Must be null when escalation_required is false.

PRACTICAL GUARDRAILS

Common Failure Modes

When an agent's internal state drifts from ground truth, every subsequent decision is compromised. These are the most common failure patterns in stale state detection and correction workflows, with concrete mitigations for each.

01

Confirmation Bias in State Verification

What to watch: The model treats its existing state as presumptively correct and interprets new observations to confirm rather than challenge it. It dismisses contradictory evidence as noise or exceptions. Guardrail: Require the prompt to explicitly list observations that conflict with current state before proposing corrections. Use a structured diff format that forces the model to articulate discrepancies before it can suggest updates.

02

Over-Correction on Single Observations

What to watch: A single tool output or observation triggers a full state rewrite, discarding accumulated evidence that may still be valid. The agent oscillates between states with each new data point. Guardrail: Include a persistence threshold in the prompt—require at least two independent observations or explicit contradiction of a previously confirmed fact before overwriting state fields. Track observation count per state element.

03

Silent State Corruption from Partial Tool Failures

What to watch: A tool returns a partial result, timeout, or malformed response, and the agent incorporates the corrupted data into its state without marking it as uncertain. Downstream steps build on broken foundations. Guardrail: Add a tool-output validation step before state ingestion. The prompt must classify each observation as complete, partial, or failed, and only complete observations can overwrite confirmed state fields. Partial results get uncertainty markers.

04

Stale Timestamps Masking Real Drift

What to watch: The agent relies on timestamp comparisons to detect staleness but fails to account for data that changes silently without timestamp updates—cached values, unversioned records, or eventually-consistent stores. Guardrail: Add a content-hash or fingerprint check alongside timestamp comparison. The prompt should compare actual field values between believed state and observed state, not just age. Flag any field where the source system does not guarantee timestamp fidelity.

05

Correction Loop Without Convergence

What to watch: The agent detects drift, applies a correction, re-observes, detects new drift from the correction, and cycles indefinitely. Each correction introduces new discrepancies that trigger further corrections. Guardrail: Cap the number of correction cycles per state element. After N failed reconciliations, the prompt must escalate to a human operator with the full diff history rather than attempting another autonomous fix. Include a stabilization check—only apply corrections when the proposed state would be stable under re-observation.

06

Dropped Context During State Diff Generation

What to watch: When generating a diff between believed and observed state, the model omits fields that are present in one snapshot but absent from the other, treating missing fields as unchanged rather than potentially deleted or unobserved. Guardrail: Require the prompt to enumerate all keys from both the believed-state and observed-state schemas in the diff output. Explicitly mark fields as added, removed, modified, or unchanged. A field absent from observation is not the same as a field confirmed unchanged.

IMPLEMENTATION TABLE

Evaluation Rubric

Use this rubric to test whether the stale state detection prompt produces safe, accurate diffs and correction actions before deploying into an agent runtime.

CriterionPass StandardFailure SignalTest Method

Stale field detection recall

All fields where [BELIEVED_STATE] differs from [OBSERVED_STATE] appear in the diff output

One or more stale fields are missing from the diff; agent proceeds with incorrect state

Compare diff output against a ground-truth diff computed from known state pairs; require 100% recall on stale fields

False-positive diff rate

No field marked as stale when [BELIEVED_STATE] and [OBSERVED_STATE] values are semantically equivalent

Equivalent values flagged as different (e.g., null vs. empty string, timestamp format differences)

Run against 20 state pairs with known semantic equivalences; require zero false-positive diffs

Correction action specificity

Every stale field has exactly one correction action with target value, no ambiguous instructions

Correction action missing target value, contains multiple conflicting updates, or says 'fix as needed'

Parse each correction action for required fields: stale_field, current_value, target_value, action_type; require 100% completeness

Over-correction prevention

Fields not present in the diff are never included in correction actions

Correction actions modify fields that were not flagged as stale; agent overwrites correct state

Diff the set of fields in correction actions against the set of fields in the diff output; require zero extra-field modifications

Confirmation bias resistance

When [OBSERVED_STATE] is incomplete or low-confidence, diff output includes uncertainty markers and does not assume observation is ground truth

Diff treats all observed values as authoritative without confidence qualifiers; missing 'needs_verification' flag on uncertain fields

Feed observed states with explicit confidence gaps; check that output contains uncertainty markers on fields where observation confidence is below threshold

Correction action ordering

Correction actions are ordered by dependency: foundational state fields corrected before dependent fields

Dependent field corrected before its prerequisite; agent applies updates in wrong order and creates inconsistent intermediate state

Check that if field B depends on field A, correction for A appears before correction for B in the action list; validate against known dependency graph

Idempotency of correction plan

Running the same diff prompt twice with identical inputs produces identical correction actions

Second run produces different correction actions, different ordering, or different target values for the same stale fields

Execute prompt twice with same [BELIEVED_STATE] and [OBSERVED_STATE]; compare JSON outputs field-by-field; require exact match on correction actions

Edge case: empty believed state

When [BELIEVED_STATE] is empty or null, all observed fields are treated as new state with initialization actions, not as corrections

Empty believed state triggers error, hallucinated diff, or treats all fields as 'stale' with delete-then-create actions

Pass empty [BELIEVED_STATE] with populated [OBSERVED_STATE]; verify output contains initialization actions with action_type 'initialize' not 'correct'

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Start with the base prompt and a simple JSON schema for the diff output. Use a single-pass comparison: feed the agent's believed state and observed state together and ask for a structured diff. Skip confidence scoring and correction-action generation initially—just get the detection working.

code
You are comparing two state snapshots. Output a JSON diff with added, removed, and changed fields.

Believed State: [BELIEVED_STATE]
Observed State: [OBSERVED_STATE]

Watch for

  • The model hallucinating changes that don't exist in either state
  • Overly granular diffs that flag formatting differences as semantic changes
  • Missing the difference between null (explicitly absent) and a missing key
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.