Prompt Chaining with AWS Lambda durable functions

Sequential LLM calls where each step builds on the previous one using AWS Lambda durable functions

This pattern demonstrates prompt chaining with AWS Lambda durable functions. Each LLM call is checkpointed, enabling automatic retry on failure and deterministic replay.
The example generates a joke about a topic, then uses a second LLM call to review and rate the joke.
Benefits: Expensive retries are avoided by resuming from the last checkpoint. Replayed LLM calls return the same response each time. Failed workflows don't restart from scratch.

Select language:

Usage Example with Python:

Prompt chaining with Lambda durable functions using Python.

import boto3
from aws_durable_execution_sdk_python import DurableContext, durable_execution

MODEL_ID = "us.amazon.nova-lite-v1:0"
bedrock = boto3.client("bedrock-runtime")


def converse(model_id: str, prompt: str) -> str:
    response = bedrock.converse(
        modelId=model_id,
        messages=[{"role": "user", "content": [{"text": prompt}]}],
    )
    return response["output"]["message"]["content"][0]["text"]


@durable_execution
def handler(event: dict, context: DurableContext):
    topic = event.get("topic", "programming")

    joke = context.step(
        lambda _: converse(MODEL_ID, f"Make a joke about {topic}"),
        "generate joke",
    )

    review = context.step(
        lambda _: converse(MODEL_ID, f'Rate this joke 1-10 and explain why: "{joke}"'),
        "review joke",
    )

    return {"joke": joke, "review": review}

                                                      



Created by:

Connor Kirkpatrick

Connor Kirkpatrick

Solution Engineer at AWS

Follow on LinkedIn