September 13, 2026

Building Bulletproof AI: Inside Microsoft Foundry’s Crash-Resilient, Long-Running Agent Architecture

building-bulletproof-ai-inside-microsoft-foundrys-crash-resilient-long-running-agent-architecture

building-bulletproof-ai-inside-microsoft-foundrys-crash-resilient-long-running-agent-architecture

Day 1 of the Microsoft Foundry 100 Days / 100 Blogs Series

Modern artificial intelligence has graduated from simple chat interfaces to autonomous, multi-step problem solvers. Yet, as developers transition from writing request-response APIs to deploying persistent, stateful agents, they are colliding with a fundamental architectural friction. Traditional cloud infrastructure—optimized for stateless, short-lived web requests—is fundamentally misaligned with agents that must run for minutes or hours, utilizing web search, executing code sandboxes, calling tools, and managing deep research loops.

For backend developers accustomed to the safety nets of standard microservices, the failure modes of long-running agents are a ticking time bomb. If a container executing a six-minute agent workflow gets aggressively OOM-killed (Out-Of-Memory killed) during a routine Kubernetes scale-in event, traditional background tasks (like Celery workers or bare asyncio.create_task routines) evaporate. The client polls for results, receives a generic failure status with zero context on intermediate progress, and the entire multi-step pipeline must be re-run from scratch.

Worse still, unhandled interruptions frequently lead to duplicate side effects—such as a billing system charging a credit card twice or an API dispatching duplicate notification emails because a previous tool execution completed just microseconds before the container crashed.

To bridge this gap, Microsoft has introduced a first-class preview capability within the Microsoft Foundry Agent Service: durable work identity, lease-based crash detection, and stream replay. Wired directly into the AgentServer software development kits (SDKs) for both the high-level Responses protocol and lower-level task primitives, this architecture promises to turn AI agents from fragile demos into production-grade enterprise systems.


Main Facts: Deconstructing Foundry’s Resilient Execution Model

At its core, Microsoft Foundry’s new architecture separates transient process lifecycles from durable workloads. The framework introduces several critical concepts to ensure that agent tasks survive infrastructure volatility—ranging from container redeploys and spot-instance evictions to node patching and sudden hardware faults.

1. Background vs. Resilient Execution

Foundry’s documentation makes a precise distinction between simply running a task asynchronously and making it genuinely crash-resilient:

  • Background Execution: Provides asynchronous processing where clients can poll or reconnect to a running task. However, if the underlying process dies, the task is marked as failed.
  • Resilient Execution: Delivers durable work identities, persisted inputs, process-loss detection, and handler reentry.
  • Stream Replay: Retains past events so that a reconnecting client can catch up seamlessly via a cursor without restarting its observation stream.

Crucially, full crash recovery for the Responses protocol only applies when requests are explicitly configured as stored and backgrounded (store=true, background=true) with the server opted into resilient_background=True. Foreground responses always fail hard on container termination because there is no remaining client connection to resume toward.

2. The Lease-Based Distributed Lock

Foundry avoids making engineers build custom Redis-backed lock managers or database job ledgers by embedding a native lease pattern directly into the agent hosting layer.

  • When a resilient task is initiated, the system issues a durable work identity coupled with a lease mechanism.
  • A worker process acquires a lease on the work record and processes the input.
  • If the worker process vanishes without releasing the lease (due to a crash or OOM kill), the lease expires automatically after a timeout window.
  • A newly spawned worker process detects the expired lease, reclaims the work record, and re-enters the agent handler.

3. Re-entry, Not Time Travel

The single most important paradigm shift for developers adopting this model is understanding that recovery re-enters the handler from the very beginning of the function.

Resilience does not act as a time machine that restores local Python variables, in-memory call stacks, or partial objects. Everything held in local memory is discarded upon a crash. The only survivor is what has been explicitly persisted to the data store. Consequently, developers must design their handlers to read from durable checkpoints upon restart.


Chronology of an Agent Failure and Recovery

To understand how these primitives operate in practice, consider the lifecycle of a three-stage agent turn—Analyze $rightarrow$ Generate $rightarrow$ Refine—running under Foundry’s resilient streaming architecture.

  1. Initialization: A client submits a request with store=true and background=true. Handler Process A acquires a lease on the durable work record from the Foundry State Store.
  2. Execution & Checkpointing: Process A successfully completes the Analyze stage. It commits a progress checkpoint to the state store before proceeding to Generate.
  3. The Catastrophe: Mid-way through the Generate stage, the orchestrator triggers an automatic scale-in event, terminating the container running Process A.
  4. Lease Expiration & Detection: The lease attached to Process A expires. Foundry’s runtime infrastructure identifies the abandoned work record.
  5. Reclamation & Re-entry: Newly spawned Handler Process B reclaims the work record. It invokes the handler function with a recovery flag (is_recovery=True).
  6. Bypassing Completed Work: The handler reads the durable state store, discovers that the Analyze stage is already marked as completed and checkpointed, and skips it entirely.
  7. Resumption: Process B resumes execution directly at the Generate stage, moves on to Refine, and finalizes the response successfully without consuming an extra application retry budget.

Supporting Data and Implementation Architecture

Implementing a crash-resilient agent using the Responses protocol requires explicitly turning on the feature flag within the server host configuration:

# main.py
from azure.ai.agentserver.responses import ResponsesAgentServerHost, ResponsesServerOptions

# resilient_background defaults to False. Without this, crashed background 
# tasks are simply marked "failed" without reinvoking the handler.
options = ResponsesServerOptions(resilient_background=True)
app = ResponsesAgentServerHost(options=options)

Within the handler logic, developers branch their code based on whether the execution context is fresh or recovered from a crash:

from azure.ai.agentserver.responses import ResponseEventStream

STAGES = ["analyze", "generate", "refine"]

@app.response_handler
async def handler(request, context, cancellation_signal):
    if context.is_recovery:
        # Rebuild stream from the last durable snapshot instead of restarting
        stream = ResponseEventStream.from_snapshot(context.persisted_response)
        start_stage = len(stream.response.output)  # Number of completed stages
    else:
        stream = ResponseEventStream(response_id=context.response_id, request=request)
        start_stage = 0

    for i in range(start_stage, len(STAGES)):
        stage = STAGES[i]

        if context.is_shutting_down:
            await context.exit_for_recovery()
            return

        result = await run_stage(stage, request.input, stream)

        # Checkpoint the output item. If the process dies after this line,
        # the recovered attempt skips this stage permanently.
        stream.checkpoint_output_item(result)

    return stream.finalize()

Managing State with FoundryStateStore

For lower-level task primitives or custom multi-turn workflows, developers can manage state via FoundryStateStore. To prevent split-brain conditions or race criteria during recovery, checkpoint writing must follow strict sequencing: write the immutable checkpoint before advancing the mutable progress marker.

from azure.ai.agentserver.core.storage import FoundryStateStore

async def save_step(task_id: str, step: int, result: dict) -> None:
    store = await FoundryStateStore.get_or_create(f"workflows/task_id")
    async with store:
        progress = await store.get_item("progress")

        if progress and int(progress.value["workflow_step"]) > step:
            return

        checkpoint_key = f"checkpoints/step"
        checkpoint = await store.get_item(checkpoint_key)

        if checkpoint is None:
            await store.create_item(checkpoint_key, result)
        elif checkpoint.value != result:
            raise RuntimeError("Checkpoint divergence detected: existing result mismatch.")

        next_progress = "workflow_step": step + 1
        if progress is None:
            await store.create_item("progress", next_progress)
        else:
            await store.set_item("progress", next_progress, if_match=progress.etag)

Official Responses and Industry Implications

The release of Foundry’s resilient agent primitives reflects a broader, industry-wide maturation. As enterprises move artificial intelligence out of experimental sandboxes and into core operational systems—such as automated supply chain auditing, financial compliance analysis, and autonomous customer negotiations—system reliability can no longer be an afterthought.

According to technical architects working within Microsoft’s ecosystem, the primary design philosophy behind this feature wave was removing "accidental complexity." Historically, teams building enterprise-grade agents had to spend weeks wiring up custom orchestration layers using tools like Temporal, Redis, and PostgreSQL just to achieve basic fault tolerance. By baking durable identities, leasing, and state stores directly into the agent hosting layer, Microsoft is standardizing how stateful AI applications are built.

Furthermore, these primitives integrate neatly with popular workflow frameworks like LangGraph and the Microsoft Agent Framework (MAF). By pointing a framework’s native checkpointer directly to FoundryStateStore, developers can achieve crash-durable state management without rewriting core agent logic. (Note: when using the MAF adapter, engineers must explicitly enable user_isolation=True to prevent multi-tenant data leaks across shared workflow definitions).


Implications for Production Engineering

While Foundry provides the underlying plumbing for crash resilience, engineering teams must still navigate complex architectural trade-offs:

  1. Fencing Non-Idempotent Side Effects: Checkpointing prevents redundant computation, but it does not automatically prevent external side effects like sending duplicate emails or executing double charges. Developers must write explicit watermarks to conversation metadata before calling non-idempotent external APIs.
  2. Graceful Shutdown Handling: During a container SIGTERM event, developers must call await context.exit_for_recovery(). Writing a terminal state (completed or failed) during this shutdown window will permanently seal the task, preventing any future worker from recovering it.
  3. Tenant Security and Isolation: Because durable work records, checkpoints, and stream states are persisted centrally, strict tenant scoping is mandatory. Using unisolated keys across multi-tenant applications risks exposing sensitive agent memory states to unauthorized users.

Conclusion

Long-running, tool-using agents have shattered the assumption that an application process and its unit of work share the same lifetime. When an agent execution outlives its underlying container, treating failure with a simple "retry from scratch" strategy results in bloated API costs, corrupted states, and broken user experiences.

Microsoft Foundry’s resilient agent primitives—combining durable work identities, lease-based crash detection, structured state stores, and stream replay—offer a robust foundation for production-grade AI. While preview software requires careful consideration before massive deployment, the underlying design pattern of re-entrant handlers, explicit checkpointing, and watermarked side effects represents the future of fault-tolerant agentic engineering.