loc bengaluru, ist | local --:-- srijanshukla18@gmail.com →
[post]/ai/putting-an-llm-inside-a-workflow-three-runtime-designs

three ways to put an LLM inside a Temporal workflow

/ 6 min read· ai

Three production runtime designs for LLM workflows, compared by who owns context, iteration, execution, validation, and safety.

Companion to running durable agent sessions with OpenAI SDK, Docker, and Temporal.

Whenever you run an LLM in prod for backend/infra work, you want a durable workflow (like Temporal) around it for retries, timeouts, audit logs, and human approvals.

What goes inside the workflow, and who owns the loop?

Every setup has the same outer pipeline:

prepare ─► context ─► model ─► validate ─► persist

Depending on if deterministic code or the model computes context and picks the next step, you land on one of three designs.

1. Single LLM call inside a deterministic workflow

prepare ─► context ─► respond ─► validate ─► persist ─► DONE

  context: code-computed · respond: one LLM call, answer out
  no tools · no iteration · no side effects
  loop: none · safety: post-hoc verify, flag for review

Workflow code does 90% of the work upfront. Code fetches logs, metrics, diffs, stuffs them into a prompt, makes one LLM call for structured JSON/markdown, validates the schema, saves it. Model has zero tools and never loops.

Good SRE/infra fits:

  • Alert triage notes: alert fires -> workflow pulls recent deploys, dashboard links, runbook sections -> 1 LLM call posts “likely cause + first 3 things to check” in Slack.
  • Postmortem first drafts: incident closes -> code pulls PagerDuty timeline, deploy events, Slack thread -> 1 LLM call drafts the markdown for the on-call to edit.
  • Change-request summaries: PR/ticket opens -> code pulls diff stats + recent outages on that service -> 1 LLM call writes a short risk summary.

Use this whenever you already know what data to fetch and what output shape you want, and a wrong answer can’t break prod.

2. Bounded specialist loops (workflow owns the loop)

admit ─► context ─► specialists ─► decide ─► publish ─► DONE

  admit: policy gate (allow / deny / approve)
  context: pinned snapshot (immutable input)
  specialists: fixed LLM roles, each looping ≤N rounds
    DRAFTER ⇄ REVIEWER ── revise on findings
    PRODUCER ⇄ CHECKS ── revise on sandbox / CI failures
    AUDITORS ×3 ── parallel, independent
  decide: policy ── proceed / revise / human / stop
  LLM proposes · code disposes

Here the model iterates on its output, but the workflow still owns the loop and runs all commands.

For example: Drafter proposes a patch/plan, Reviewer critiques it (max N rounds), Producer emits a git diff, deterministic workflow code applies that diff in a sandbox and runs terraform plan / unit tests / OPA checks. Check fails -> workflow feeds the error back to Producer for another round. Checks pass -> workflow opens a draft PR for a human to merge.

Model never gets a raw shell tool and never decides what stage runs next. It only emits text/diffs, deterministic code runs the sandbox checks between stages.

Good SRE/infra fits:

  • Policy-violation fix PRs: OPA/conftest flags a public S3 bucket or missing k8s limits -> workflow loops Drafter/Producer until terraform plan + policy checks pass in sandbox -> opens draft PR.
  • CVE / dependency bumps: scanner flags a vulnerable lib -> workflow generates bump + code fixes, runs CI test suite against that commit, opens PR with rollback notes.
  • Terraform drift reconciliation: drift detector spots live AWS state diverging from git -> workflow generates reconciling diff, gates on terraform plan, routes destructive plans to human approval.

Use this when you know what artifact you want at the end (like a working PR diff) and have an automated test/plan command to verify it.

3. Full agent loop in a sandbox (model owns the loop)

prepare ─► context ─► observe⇄DECIDE⇄ACT ─► validate ─► DONE

  observe ─► DECIDE ─► ACT
  tools: shell · http · files · browser · APIs
  risky action? ─► WAIT_FOR_APPROVAL ─► resume
  loop: model-owned, open · context: gathered by the model
  safety: sandbox boundary · approval before side effects

Here you don’t know upfront which logs, metrics, or files need checking. Put the agent in an isolated sandbox, give it read-only tools (kubectl get/describe, log search, PromQL, file search), let the model decide what to run next based on what it finds.

Temporal is the outer cage: provisions/destroys the ephemeral container, enforces hard caps on turns/tokens/runtime, and intercepts any write or risky tool call to pause on a durable WAIT_FOR_APPROVAL signal until a human approves.

Good SRE/infra fits:

  • Incident investigation: alert fires -> agent runs read-only PromQL, checks pod logs, inspects the 14:05 deploy diff, tests hypotheses, posts root-cause writeup with links to exact logs/graphs. Only asks for approval if it wants to roll back or restart pods.
  • AWS cost-spike forensics: billing alert fires -> agent digs through Cost Explorer, tags, deploy history across accounts to find what spiked.
  • Noisy-neighbor / latency debugging: latency jumps on a k8s node -> agent inspects per-pod CPU throttling, cgroup metrics, recent scheduling events to find the offending pod.

Use this when you don’t know the investigation path upfront so a hardcoded pipeline won’t cut it.

Side by side

1  function, no loop:
prepare ─► context ─► respond ─► validate ─► persist

2  specialists, bounded workflow loop:
admit ─► context ─► DRAFTER ⇄ REVIEWER ─► decide ─► publish

3  agent, open model loop:
prepare ─► context ─► observe⇄DECIDE⇄ACT ─► validate ─► DONE
Who owns the loop?Who gathers context?What the model can doWhat the model cannot do
1 - FunctionNobody (1 call)Workflow codeReturn one structured responseLoop, call tools, or run commands
2 - SpecialistsWorkflow (≤ N rounds)Workflow code + test/reviewer outputPropose & revise diffs/artifactsRun arbitrary shell commands or pick the next stage
3 - AgentThe modelThe model (via tools)Run read tools, explore, form hypothesesEscape the sandbox or run write actions without hitting an approval gate

Nice thing about building all three on the same Temporal worker setup is you don’t need three different platforms. Default to Design 1 whenever you can, Design 2 when you need code/diff generation verified by CI, and save Design 3 for open-ended debugging where the model genuinely has to explore.

Implementation details for Design 3 are in the companion post: running durable agent sessions with OpenAI SDK, Docker, and Temporal.