Inferensys

Prompt

Stale Context Detection and Recovery Prompt

A practical prompt playbook for using the Stale Context Detection and Recovery Prompt in production multi-agent workflows.
Developer demonstrating multi-agent tool use, agent tool selection interface on laptop, casual tech demo moment.
PROMPT PLAYBOOK

When to Use This Prompt

Defines the precise operational context for the Stale Context Detection and Recovery Prompt, clarifying its role in agent orchestration and its boundaries.

This prompt is a critical safety component for platform engineers building multi-agent orchestration systems. Its primary job is to act as a circuit breaker between agents, preventing a downstream agent from acting on context that has been invalidated by an upstream failure. The ideal user is an orchestration engine, a handoff middleware service, or a reliability workflow that programmatically invokes this prompt before passing state to the next agent in a dependency chain. The required context includes the original handoff payload, the upstream agent's failure reason (e.g., timeout, tool error, exception), and the expected freshness parameters (e.g., max staleness in seconds, required fields).

You should wire this prompt into the on_failure or on_timeout hook of your agent execution graph. Before the orchestrator routes work to Agent_B, it must call this prompt with the state Agent_A produced and the error Agent_A threw. The prompt's structured output—a staleness assessment, a safe_to_proceed boolean, and a context-reconstruction request—becomes the control signal. If safe_to_proceed is false, the orchestrator must not forward the context and should instead route to a fallback agent, a human review queue, or a dedicated recovery agent using the provided reconstruction request. This is not a passive logging step; it is an active gating mechanism.

Do not use this prompt as a general-purpose agent health check or a replacement for standard agent-level timeouts and retries. It is specifically designed for the handoff boundary where corrupted state can propagate. Avoid using it for real-time, latency-sensitive streaming workflows where the assessment overhead would violate a user-facing SLA. The next step after integrating this prompt is to pair it with the 'Fallback Agent Context Injection Prompt' to ensure any recovery agent receives a clean, explicitly bounded context package, not the original corrupted state.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Stale Context Detection and Recovery Prompt delivers value and where it introduces risk.

01

Good Fit: Multi-Agent Pipelines

Use when: An agent receives state from an upstream agent that may have failed, timed out, or produced partial results. Guardrail: Always include the upstream agent's execution timestamp and expected freshness window in the prompt input.

02

Good Fit: Long-Running Workflows

Use when: A task spans minutes or hours and cached context may have been invalidated by external changes. Guardrail: Pair this prompt with a context version marker or sequence number to detect drift deterministically before relying on the model's assessment.

03

Bad Fit: Real-Time Streaming

Avoid when: Latency budget is under 500ms or the context is ephemeral and unrecoverable. Guardrail: Use a deterministic freshness check in the application layer for low-latency paths; reserve this prompt for asynchronous recovery flows.

04

Required Inputs

Requires: The stale context payload, the upstream agent's identity and last-success timestamp, the current task description, and the maximum acceptable staleness threshold. Guardrail: Validate that all required fields are present before invoking the prompt; missing inputs produce unreliable staleness assessments.

05

Operational Risk: False Confidence

Risk: The model declares context 'fresh enough' when critical data has changed, leading to downstream decisions on bad state. Guardrail: Never rely solely on the model's safe-to-proceed determination for high-risk actions; require a secondary application-level freshness check or human approval for irreversible operations.

06

Operational Risk: Unnecessary Aborts

Risk: The model overestimates staleness and aborts work that could have proceeded safely, causing pipeline churn and user-visible delays. Guardrail: Include explicit examples of borderline-fresh context in few-shot demonstrations to calibrate the model's abort-vs-proceed threshold.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A copy-ready prompt for detecting stale context from a failed upstream agent and generating a safe recovery or abort decision.

This prompt is the core instruction set you will inject into your orchestration layer when an agent receives context from a failed or timed-out upstream agent. Its job is not to complete the original task, but to perform a safety gate check: assess whether the received state is stale, determine if it is safe to proceed with degraded information, and if so, request exactly what is needed to reconstruct a viable working context. The prompt is designed to be stateless and idempotent; it should be called immediately after a handoff failure is detected, before any downstream work begins.

text
You are a Stale Context Detector and Recovery Agent. Your sole responsibility is to evaluate the context received from a failed upstream agent and decide whether it is safe for a downstream agent to proceed.

## INPUT
[UPSTREAM_AGENT_ID]: The identifier of the agent that failed.
[FAILURE_REASON]: The error code, timeout message, or exception from the upstream agent.
[RECEIVED_CONTEXT]: The full state object or message payload handed off by the failed agent.
[ORIGINAL_USER_INTENT]: The user's initial request before any agent processing began.
[DOWNSTREAM_TASK]: The specific task the next agent is expected to perform.
[STALENESS_THRESHOLD_SECONDS]: Maximum age of context before it is considered stale.

## TASK
1.  **Staleness Assessment**: Determine if the [RECEIVED_CONTEXT] is stale based on the [FAILURE_REASON] and [STALENESS_THRESHOLD_SECONDS]. Check for missing critical fields, outdated timestamps, or partial results that conflict with the [ORIGINAL_USER_INTENT].
2.  **Safe-to-Proceed Determination**: Decide if the [DOWNSTREAM_TASK] can be completed safely using only the [RECEIVED_CONTEXT]. If the context is incomplete but the missing information is non-critical for the specific downstream task, it may be safe to proceed with a degraded flag.
3.  **Context-Reconstruction Request**: If it is NOT safe to proceed, generate a structured request for the missing information. This request must be answerable by a human operator or a retry of the upstream agent.

## OUTPUT_SCHEMA
You must respond with a single JSON object conforming to this schema:
{
  "staleness_assessment": {
    "is_stale": boolean,
    "confidence": float (0.0 to 1.0),
    "reasons": [string, ...]
  },
  "safe_to_proceed": boolean,
  "degraded_mode": boolean,
  "recovery_action": "proceed" | "abort_and_escalate" | "reconstruct_context",
  "context_reconstruction_request": {
    "missing_fields": [string, ...],
    "specific_questions": [string, ...],
    "suggested_retry_payload": object | null
  },
  "handoff_summary_for_human": string
}

## CONSTRAINTS
- Never fabricate missing data to make the context appear complete.
- If [RECEIVED_CONTEXT] contains contradictory information, flag it as stale and unsafe.
- The `handoff_summary_for_human` must be a concise, jargon-free explanation of what failed and what is needed next.
- If `safe_to_proceed` is true but `degraded_mode` is also true, explicitly list which capabilities will be unavailable to the downstream agent.

To adapt this template, replace the square-bracket placeholders at runtime from your agent orchestration framework's error handler. The [RECEIVED_CONTEXT] should be the raw payload from the failed agent, not a pre-parsed summary. The [DOWNSTREAM_TASK] should be a verbatim copy of the task instruction the next agent was supposed to receive. For high-risk domains, always route the handoff_summary_for_human to an on-call channel when recovery_action is abort_and_escalate. Validate the model's output against the JSON schema before allowing any downstream agent to consume the safe_to_proceed boolean; a parse failure should be treated as an implicit abort_and_escalate.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the Stale Context Detection and Recovery Prompt. Each placeholder must be populated before the prompt is sent. Missing or malformed inputs cause false staleness flags or unsafe proceed decisions.

PlaceholderPurposeExampleValidation Notes

[CURRENT_AGENT_STATE]

The state snapshot the agent is about to act on, including timestamps and source agent IDs

{"last_updated": "2025-01-15T08:22:00Z", "source_agent": "order-lookup-v2", "order_status": "pending"}

Must include last_updated field. Reject if timestamp is missing or unparseable. Compare against wall-clock time to compute staleness delta.

[UPSTREAM_AGENT_HEALTH]

Health status of the upstream agent that produced the state, including failure history and last successful heartbeat

{"agent_id": "order-lookup-v2", "status": "degraded", "last_heartbeat": "2025-01-15T08:20:00Z", "consecutive_failures": 3}

Must include status enum (healthy, degraded, unavailable). Reject if agent_id does not match source_agent in CURRENT_AGENT_STATE. Null allowed if upstream health is unknown.

[STALENESS_THRESHOLD_SECONDS]

Maximum acceptable age of context before it is considered stale, in seconds

300

Must be a positive integer. Reject if zero or negative. Default to 300 if not specified. Domain-specific thresholds (e.g., 60 for financial data) should be set explicitly.

[TASK_CRITICALITY]

Classification of how critical the current task is, used to determine whether to proceed with stale context or abort

high

Must be one of: low, medium, high, critical. Reject unrecognized values. Critical tasks should never proceed with stale context. Low-criticality tasks may proceed with explicit staleness disclosure.

[DOWNSTREAM_AGENTS]

List of agents that depend on this agent's output, used to assess blast radius of a wrong decision

["shipping-agent-v1", "notification-agent-v3"]

Must be an array of agent ID strings. Empty array allowed if no downstream dependencies. Used to populate impact assessment in recovery request.

[CONTEXT_SCHEMA]

Expected schema of the context fields, used to detect missing or corrupted fields beyond staleness

{"required_fields": ["order_id", "customer_id", "order_status"], "optional_fields": ["tracking_number"]}

Must include required_fields array. Validate that CURRENT_AGENT_STATE contains all required_fields. Missing required fields trigger immediate abort regardless of staleness.

[RECOVERY_AGENT_POOL]

Available agents that can reconstruct or refresh the stale context, with capability descriptions

["order-lookup-v2", "order-lookup-fallback-v1", "human-ops-queue"]

Must be a non-empty array. First entry is preferred recovery agent. Last entry should be a human escalation path. Reject if pool is empty when staleness is detected.

PROMPT PLAYBOOK

Implementation Harness Notes

Wire the staleness assessment into your agent orchestration layer as a pre-execution guard, branching on the safe_to_proceed field.

Integrate this prompt as a pre-execution guard in your agent orchestration layer. Before a downstream agent consumes context from an upstream dependency, check the upstream agent's exit status. If the status indicates failure, timeout, or partial_completion, invoke this prompt with the stale context and the upstream agent's error metadata. Do not invoke it on every agent call—only on known failure signals. The prompt adds latency and token cost, so reserve it for paths where stale context would cause cascading errors or silent data corruption.

Parse the JSON output and branch on the safe_to_proceed field. If true, pass the assessed_context to the downstream agent and log the staleness_score and risk_factors to your agent trace store. If false, route the recovery_request to the specified recovery agent or escalate to a human operator via your incident management system. Validation checks to implement in the harness: confirm staleness_score is a float between 0.0 and 1.0; verify safe_to_proceed is a strict boolean; ensure risk_factors is a non-empty array when safe_to_proceed is false; and validate that recovery_request.target_agent matches a known agent ID in your registry. Reject malformed outputs and retry once with a stricter temperature setting before escalating.

Model choice and logging: Use a fast, instruction-following model (e.g., Claude 3.5 Haiku, GPT-4o-mini) for this guard—it's a classification and structured generation task, not a reasoning-heavy one. Log the full staleness assessment, including upstream_agent_id, failure_type, context_age_seconds, staleness_score, and the branch decision, to your agent trace store for postmortem analysis. This trace data is essential for tuning your staleness thresholds over time and identifying which upstream agents produce the most stale-context events. Avoid: using this prompt as a substitute for proper agent health checks or timeout configurations—it is a recovery mechanism, not a prevention tool.

IMPLEMENTATION TABLE

Expected Output Contract

Defines the exact fields, types, and validation rules for the staleness assessment response. Use this contract to parse, validate, and route the model output before acting on it.

Field or ElementType or FormatRequiredValidation Rule

staleness_detected

boolean

Must be true or false. If true, at least one staleness_indicator must be present.

staleness_indicators

array of strings

Each entry must match an enum value: context_timestamp_mismatch, upstream_agent_failure, data_version_conflict, missing_expected_field, stale_session_id, user_intent_change, tool_output_expired.

staleness_severity

string

Must be one of: safe_to_proceed, degraded_but_usable, unsafe_to_proceed. If unsafe_to_proceed, abort_reason is required.

safe_to_proceed

boolean

Must align with staleness_severity: true only for safe_to_proceed; false for unsafe_to_proceed; may be true or false for degraded_but_usable.

abort_reason

string or null

Required when safe_to_proceed is false. Must cite at least one staleness_indicator and explain why proceeding is unsafe.

context_reconstruction_request

object or null

Required when safe_to_proceed is false. Must contain target_agent, required_fields (array), and priority (low, medium, high, critical).

confidence_score

number

Float between 0.0 and 1.0. Scores below 0.5 must trigger human review if safe_to_proceed is true.

human_review_recommended

boolean

Must be true if confidence_score < 0.5 or staleness_severity is unsafe_to_proceed. False otherwise.

PRACTICAL GUARDRAILS

Common Failure Modes

Stale context is one of the most dangerous failure modes in multi-agent systems because the receiving agent often cannot distinguish between fresh and outdated information. These cards cover the most common failure patterns and how to guard against them.

01

Silent Staleness Acceptance

What to watch: The receiving agent processes stale context without detecting it, producing confident but incorrect output based on outdated state. This happens when staleness detection is left to the agent's judgment without explicit checks. Guardrail: Require the prompt to extract and compare timestamps, sequence numbers, or version markers before reasoning. Add a mandatory staleness assessment step before any task execution.

02

False Freshness from Missing Metadata

What to watch: Context arrives without timestamps, version identifiers, or generation markers, making staleness impossible to detect programmatically. The agent assumes freshness by default. Guardrail: Enforce a context envelope schema that requires generated_at, source_agent_id, and context_version fields. Reject or flag context missing these fields before it reaches the reasoning agent.

03

Partial Update Contamination

What to watch: An upstream agent updates only some fields in a shared context object, leaving other fields stale. The downstream agent treats the entire object as current. Guardrail: Implement per-field staleness tracking with individual timestamps or version vectors. The staleness detection prompt should assess each field independently and flag mixed-freshness objects.

04

Overly Aggressive Abort Decisions

What to watch: The staleness detection prompt is calibrated too conservatively, aborting tasks when context is only slightly stale but still usable. This creates unnecessary degradation and user-visible failures. Guardrail: Define staleness thresholds with explicit tolerance windows. Include a safe_to_proceed_with_caveats option that allows the agent to continue with explicit uncertainty markers rather than binary abort-or-proceed.

05

Context Reconstruction Loop Failures

What to watch: The recovery prompt requests fresh context from an upstream agent that is also degraded or unavailable, creating an infinite retry loop or cascading timeout. Guardrail: Cap context reconstruction attempts at a fixed number with exponential backoff. After the cap, escalate to a human operator with the full staleness report and reconstruction attempt history rather than retrying silently.

06

Staleness Drift Across Parallel Agents

What to watch: Multiple downstream agents receive context at slightly different times, leading to inconsistent staleness assessments and conflicting decisions across the agent graph. Guardrail: Use a centralized context version clock or watermark that all agents reference. The staleness detection prompt should compare against this shared watermark rather than wall-clock time to ensure consistent assessments across the system.

IMPLEMENTATION TABLE

Evaluation Rubric

Use this rubric to evaluate the Stale Context Detection and Recovery Prompt before shipping. Each criterion targets a specific failure mode in multi-agent staleness detection.

CriterionPass StandardFailure SignalTest Method

Staleness Flag Accuracy

Correctly flags context as stale when upstream agent version or timestamp is older than the current workflow state

Prompt returns is_stale: false when upstream agent context is demonstrably outdated

Run against a golden set of 20 context pairs where 10 are stale and 10 are fresh; require >= 95% accuracy

Safe-to-Proceed Determination

Returns proceed: false when any critical field in the stale context is missing, contradictory, or expired

Prompt returns proceed: true when a required field like [USER_INTENT] or [AUTH_STATE] is null or expired

Inject stale context with deliberately missing critical fields; assert proceed is false and reason field is populated

Context-Reconstruction Request Quality

Generates a reconstruction request that specifies exactly which fields are needed, their expected sources, and priority

Reconstruction request is generic (e.g., 'send all context again') or omits field-level specificity

Parse the reconstruction_request field; assert it contains at least one specific field name from the original context schema and a source hint

Abort-vs-Proceed Decision Rationale

Includes a clear, evidence-based reason for the abort or proceed decision referencing specific stale fields

Decision rationale is missing, circular, or references fields not present in the stale context

Check that the rationale field cites at least one stale field by name and explains why it blocks or allows continuation

Upstream Agent Identification

Correctly identifies which upstream agent produced the stale context from the provided agent chain metadata

Prompt misidentifies the source agent or returns null when agent metadata is present in [AGENT_CHAIN]

Provide agent chain metadata with 3 agents; assert the identified agent matches the actual stale context producer

False Positive Rate on Fresh Context

Returns is_stale: false and proceed: true for context that is current and complete

Prompt flags fresh context as stale, triggering unnecessary reconstruction

Run against 10 fresh context samples; require is_stale: false in all cases

Timeout-Aware Staleness

Considers the [MAX_CONTEXT_AGE_SECONDS] threshold and flags context exceeding it even if otherwise complete

Prompt ignores the age threshold and treats expired-but-complete context as fresh

Set [MAX_CONTEXT_AGE_SECONDS] to 60 and provide context aged 120 seconds; assert is_stale: true

Output Schema Compliance

Returns a valid JSON object matching the [OUTPUT_SCHEMA] with all required fields present and correctly typed

Output is missing required fields, contains extra hallucinated fields, or uses wrong types

Validate output against the JSON schema; assert no missing required fields and no additional properties beyond schema

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Use the base prompt with a single model and log staleness assessments manually. Drop the structured output schema initially and observe whether the model correctly identifies stale fields and recommends safe-to-proceed vs. abort decisions. Focus on getting the staleness flag and context-reconstruction request right before adding validation.

Watch for

  • Over-flagging: treating any missing field as stale rather than distinguishing truly outdated state from intentionally empty values
  • Missing the timestamp or sequence-number comparison that proves staleness
  • Vague reconstruction requests that don't specify which upstream agent and which fields are needed
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.