DEV Community

Sanjay Vadhiya
Sanjay Vadhiya

Posted on

AI + Temporal in .NET 10: Building Reliable AI Workflows

AI applications are no longer just:

Request
   ↓
AI Model
   ↓
Response
Enter fullscreen mode Exit fullscreen mode

Real-world AI applications often involve multiple steps:

User Request
     ↓
AI Analysis
     ↓
External API
     ↓
Database
     ↓
Human Approval
     ↓
Final Action
Enter fullscreen mode Exit fullscreen mode
  • But what happens if the AI API fails?
  • What if the application crashes halfway through the workflow?
  • What if a human needs to approve something tomorrow?

This is where Temporal can be useful.

AI provides intelligence. Temporal provides reliable workflow execution.


Why Combine AI with Temporal?

Imagine an AI-powered order review system:

Order Created
     ↓
Charge Payment
     ↓
AI Risk Analysis
     ↓
Check Risk
   /       \
Low Risk   High Risk
   ↓          ↓
Continue   Human Review
              ↓
          Approval
              ↓
          Continue
Enter fullscreen mode Exit fullscreen mode

There are several things that can fail:

  • AI API can be unavailable
  • External APIs can timeout
  • Workers can restart
  • A workflow may need to wait for a human
  • An AI operation may need to be retried

Instead of writing custom infrastructure for all of these situations, Temporal can manage the workflow execution.


The Basic Architecture

A simple AI + Temporal architecture looks like this:

              ASP.NET Core
                   ↓
             Temporal Server
                   ↓
              .NET Worker
                   ↓
            Temporal Workflow
                   ↓
        ┌──────────┼──────────┐
        ↓          ↓          ↓
       AI       Database    External API
    Activity     Activity      Activity
Enter fullscreen mode Exit fullscreen mode

The important separation is:

Workflow
   ↓
Controls the process

Activity
   ↓
Performs external work
Enter fullscreen mode Exit fullscreen mode

The AI call should generally be placed inside an Activity because it communicates with an external service.


AI as a Temporal Activity

For example:

using Temporalio.Activities;

public class AiActivities
{
    [Activity]
    public async Task<string> AnalyzeOrderAsync(
        string description,
        decimal amount)
    {
        // Call your AI provider here.
        // OpenAI, Gemini, Azure OpenAI,
        // Amazon Bedrock, etc.

        await Task.Delay(500);

        return amount > 10000
            ? "HIGH_RISK"
            : "LOW_RISK";
    }
}
Enter fullscreen mode Exit fullscreen mode

The Workflow can then call this Activity:

[Workflow]
public class OrderWorkflow
{
    [WorkflowRun]
    public async Task<string> RunAsync(
        string description,
        decimal amount)
    {
        var risk =
            await Workflow.ExecuteActivityAsync(
                (AiActivities activities) =>
                    activities.AnalyzeOrderAsync(
                        description,
                        amount),
                new()
                {
                    StartToCloseTimeout =
                        TimeSpan.FromMinutes(5)
                });

        return risk;
    }
}
Enter fullscreen mode Exit fullscreen mode

The Workflow controls when the AI should run.

The Activity performs the actual AI call.


What Happens When AI Fails?

Suppose the AI provider temporarily returns an error:

AI Activity
     ↓
503 Service Unavailable
Enter fullscreen mode Exit fullscreen mode

Instead of immediately losing the workflow, Temporal can retry the Activity according to a configured retry policy.

Conceptually:

Attempt 1 → Failed
     ↓
   Retry
     ↓
Attempt 2 → Failed
     ↓
   Retry
     ↓
Attempt 3 → Success
Enter fullscreen mode Exit fullscreen mode

This becomes especially useful when your workflow depends on multiple external services.

Of course, not every error should be retried. Production applications should define appropriate retry policies for rate limits, timeouts, temporary failures, and permanent errors.


AI + Human-in-the-Loop

One of the most useful patterns is combining AI with human approval.

For example:

AI Analysis
     ↓
Risk Check
     ↓
High Risk
     ↓
Human Review
     ↓
WAIT
     ↓
Manager Approves
     ↓
Workflow Continues
Enter fullscreen mode Exit fullscreen mode

The manager might approve the order several hours or even days later.

Temporal supports mechanisms such as Signals that allow external applications to communicate with a running workflow.

This means you don't need to keep a .NET thread running while waiting for the manager.

The workflow can wait for the event and continue when the approval arrives.


AI Agents + Temporal

This idea can also be extended to AI agents.

Imagine an AI agent that needs to call several tools:

User Request
     ↓
AI Agent
     ↓
Search API
     ↓
Database
     ↓
Payment API
     ↓
AI Evaluation
     ↓
Final Result
Enter fullscreen mode Exit fullscreen mode

Temporal can orchestrate this process and provide durable execution around the individual operations.

This is particularly useful when an AI agent performs important business operations rather than simply generating text.


When Should You Use Temporal with AI?

Temporal can be a good choice when your AI workflow is:

  • Multi-step
  • Long-running
  • Dependent on external services
  • Failure-prone
  • Waiting for human approval
  • Performing important business operations
  • Required to survive application restarts

For a simple application like:

HTTP Request
     ↓
AI API
     ↓
Response
Enter fullscreen mode Exit fullscreen mode

Temporal may be unnecessary.

The goal isn't to add Temporal to every AI application.

The goal is to use it when the workflow itself needs reliable execution.


A Simple Mental Model

Think about the responsibilities like this:

AI
 ↓
"Analyze this."

Temporal
 ↓
"Make sure this entire process reliably executes."

ASP.NET Core
 ↓
"Expose the application and APIs."
Enter fullscreen mode Exit fullscreen mode

AI handles intelligence.

Temporal handles orchestration and durable execution.

ASP.NET Core handles the application layer.


Final Thoughts

AI is becoming an important part of modern .NET applications, but production AI systems need more than a good model.

They also need reliable workflows.

That's where Temporal becomes interesting.

AI
 +
Temporal
 +
.NET
Enter fullscreen mode Exit fullscreen mode

can be used to build workflows that combine AI models, databases, APIs, messaging systems, and human approval while handling failures and long-running processes.

The key takeaway:

Don't make AI responsible for the entire workflow. Let AI provide intelligence and let Temporal orchestrate the process reliably.

That combination can be especially powerful for AI-powered applications where reliability matters as much as intelligence.

Top comments (0)