[{"data":1,"prerenderedAt":58},["ShallowReactive",2],{"snippet-lambda-durable-ai-llm-as-judge":3},{"id":4,"title":5,"contributors":6,"description":8,"extension":9,"gitHub":10,"introBox":13,"languages":19,"meta":22,"resources":23,"services":28,"snippets":31,"stem":49,"tags":50,"type":56,"__hash__":57},"snippets\u002Fsnippets\u002Flambda-durable-ai-llm-as-judge.json","LLM as Judge with AWS Lambda durable functions",[7],"content\u002Fcontributors\u002Fconnor-kirkpatrick.json","Get multiple AI responses in parallel, then use another LLM to pick the best one using AWS Lambda durable functions","json",{"template":11},{"repoURL":12},"https:\u002F\u002Fgithub.com\u002Faws-samples\u002Fsample-ai-workflows-in-aws-lambda-durable-functions",{"headline":14,"text":15},"How it works",[16,17,18],"This pattern demonstrates the LLM-as-judge pattern with AWS Lambda durable functions. Multiple models answer a question in parallel, then another LLM evaluates and selects the best response.","All candidate answers and the final judgment are checkpointed for deterministic replay.","Benefits: Parallel execution reduces total workflow time. Checkpointing prevents re-running expensive LLM calls on retry. Deterministic replay ensures consistent results.",[20,21],"Python","TypeScript",{},{"bullets":24},[25],{"text":26,"link":27},"AWS Lambda durable functions","https:\u002F\u002Faws.amazon.com\u002Flambda\u002Flambda-durable-functions\u002F",[29,30],"lambda-event-functions","bedrock",[32],{"title":33,"codeTabs":34},"Runtimes",[35,42],{"id":20,"title":36,"description":37,"snippets":38},"Usage Example with Python:","LLM as judge with Lambda durable functions using Python.",[39],{"language":40,"code":41},"py","import json\nimport re\n\nimport boto3\nfrom aws_durable_execution_sdk_python import DurableContext, durable_execution\n\nMODELS = [\"us.amazon.nova-lite-v1:0\", \"us.amazon.nova-pro-v1:0\"]\nbedrock = boto3.client(\"bedrock-runtime\")\n\n\ndef converse(model_id: str, prompt: str) -&#62; str:\n    response = bedrock.converse(\n        modelId=model_id,\n        messages=[{\"role\": \"user\", \"content\": [{\"text\": prompt}]}],\n    )\n    return response[\"output\"][\"message\"][\"content\"][0][\"text\"]\n\n\n@durable_execution\ndef handler(event: dict, context: DurableContext):\n    question = event.get(\n        \"question\", \"Write a 100 word summary on the great fire of London\"\n    )\n\n    result = context.map(\n        MODELS,\n        lambda ctx, model_id, idx, items: {\n            \"modelId\": model_id,\n            \"answer\": converse(model_id, question),\n        },\n        \"Get candidate answers\",\n    )\n\n    candidates = result.get_results()\n\n    def judge(_):\n        responses = \"\\n\".join(\n            f\"{i + 1}. (Model: {c['modelId']}) {c['answer']}\"\n            for i, c in enumerate(candidates)\n        )\n        prompt = f'''Question: \"{question}\"\n\nResponses:\n{responses}\n\nWhich response is best? Reply with JSON: {{\"bestIndex\": &#60;1-based index&#62;, \"reasoning\": \"&#60;why&#62;\"}}'''\n\n        response = converse(MODELS[0], prompt)\n        match = re.search(r\"\\{[\\s\\S]*\\}\", response)\n        parsed = json.loads(match.group(0)) if match else {}\n        best_index = (parsed.get(\"bestIndex\", 1)) - 1\n        best = candidates[best_index] if 0 &#60;= best_index &#60; len(candidates) else candidates[0]\n\n        return {\n            \"bestAnswer\": best[\"answer\"],\n            \"reasoning\": parsed.get(\"reasoning\", \"\"),\n            \"sourceModel\": best[\"modelId\"],\n        }\n\n    judgment = context.step(judge, \"judge\")\n\n    return {\"question\": question, **judgment}\n",{"id":21,"title":43,"description":44,"snippets":45},"Usage Example with TypeScript:","LLM as judge with Lambda durable functions using TypeScript.",[46],{"language":47,"code":48},"ts","import {\n  type DurableContext,\n  withDurableExecution,\n} from \"@aws\u002Fdurable-execution-sdk-js\";\nimport {\n  BedrockRuntimeClient,\n  ConverseCommand,\n} from \"@aws-sdk\u002Fclient-bedrock-runtime\";\n\nconst MODELS = [\"us.amazon.nova-lite-v1:0\", \"us.amazon.nova-pro-v1:0\"];\nconst bedrock = new BedrockRuntimeClient({});\n\nasync function converse(modelId: string, prompt: string): Promise&#60;string&#62; {\n  const response = await bedrock.send(\n    new ConverseCommand({\n      modelId,\n      messages: [{ role: \"user\", content: [{ text: prompt }] }],\n    }),\n  );\n  return response.output?.message?.content?.[0].text ?? \"\";\n}\n\nexport const handler = withDurableExecution(\n  async (event: { question?: string }, context: DurableContext) =&#62; {\n    const question =\n      event.question ?? \"Write a 100 word summary on the great fire of London\";\n\n    const result = await context.map(\n      \"Get candidate answers\",\n      MODELS,\n      async (_, modelId) =&#62; ({\n        modelId,\n        answer: await converse(modelId, question),\n      }),\n      { itemNamer: (item) =&#62; `candidate-${item}` }\n    );\n\n    const candidates = result.getResults();\n\n    const judgment = await context.step(\"judge\", async () =&#62; {\n      const prompt = `Question: \"${question}\"\n\nResponses:\n${candidates.map((r, i) =&#62; `${i + 1}. (Model: ${r.modelId}) ${r.answer}`).join(\"\\n\")}\n\nWhich response is best? Reply with JSON: {\"bestIndex\": &#60;1-based index&#62;, \"reasoning\": \"&#60;why&#62;\"}`;\n\n      const response = await converse(MODELS[0], prompt);\n      const parsed = JSON.parse(response.match(\u002F\\{[\\s\\S]*\\}\u002F)?.[0] ?? \"{}\");\n      const bestIndex = (parsed.bestIndex ?? 1) - 1;\n      const best = candidates[bestIndex] ?? candidates[0];\n\n      return {\n        bestAnswer: best.answer,\n        reasoning: parsed.reasoning ?? \"\",\n        sourceModel: best.modelId,\n      };\n    });\n\n    return { question, ...judgment };\n  }\n);\n","snippets\u002Flambda-durable-ai-llm-as-judge",[51,52,30,53,54,55],"ai","durable-functions","genai","llm-as-judge","evaluation","Integration","lx2foHtN8-JITQppuvHZ1VqNyohsEb5s00QnTvLSlAs",1792578864374]