Agentic Workflows
Learn how to build intelligent agentic workflows that make decisions and automate workflows with Motia
What You'll Build
An intelligent pet management system with agentic workflows that automate decisions and enrich data:
- AI Profile Enrichment - Automatically generates detailed pet profiles using AI
- Health Review Agentic Step - Makes intelligent health decisions based on symptoms
- Adoption Review Agentic Step - Assesses adoption readiness and data completeness
- Orchestrator Integration - AI decisions that drive real workflow changes
Getting Started
Clone the example repository:
Install dependencies:
Set up your OpenAI API key in .env:
Important! This tutorial requires an OpenAI API key. Get yours at platform.openai.com/api-keys. Without it, the agentic workflows won't work.
Start the Workbench:
Your Workbench will be available at http://localhost:3000.
Project Structure
Files like features.json and tutorial/tutorial.tsx are only for the interactive tutorial and are not part of Motia's project structure.
All code examples in this guide are available in the build-your-first-app repository.
You can follow this guide to learn how to build agentic workflows with Motia step by step, or you can clone the repository and dive into our Interactive Tutorial to learn by doing directly in the Workbench.

Understanding Agentic Workflows
You've built APIs, background jobs, and workflows that orchestrate your pet shelter. But what about decisions that aren't black and white? Should this pet's symptoms require treatment? Is this profile ready for the adoption page?
That's where agentic workflows come in. They're smart assistants that make judgment calls based on context - the kind of decisions that would normally need a human to review every single case.
In our pet shelter, we use two flavors:
- Content generators write engaging pet profiles automatically
- Decision makers evaluate health symptoms and choose whether treatment is needed
- Data reviewers assess if adoption information is complete
The difference from traditional code? Instead of writing hundreds of if-else rules for every possible symptom combination, you describe what matters to the AI. It reads the context and makes an informed call.
When a pet arrives with "coughing, lethargy, loss of appetite" - the AI evaluates these symptoms together and decides if treatment is needed. No hardcoded rules. Just intelligent analysis of the situation.
Creating Your First Agentic Step
Let's start with a content generation agentic step that automatically enriches pet profiles when they're created.
Step 1: Set Up Pet Creation to Emit Events
First, update your pet creation endpoint to emit events that will trigger the agentic step.
View on GitHub:
The API endpoint now emits an event after creating a pet. The response returns immediately while the agentic step processes asynchronously in the background.
Step 2: Create the AI Profile Enrichment Agentic Step
Now let's create the agentic step that listens for new pets and enriches their profiles with AI-generated content.
View on GitHub:
How This Agentic Step Works
This is a content generation agentic step - it enriches data without making workflow decisions:
- Subscribes to
pet.createdevents - Calls OpenAI with a carefully crafted prompt
- Parses the response into structured data
- Updates the pet with AI-generated content
- Has a fallback if the AI call fails
The key is the prompt engineering - we tell the AI exactly what fields we need and what tone to use. The AI returns JSON that we can parse and store directly.
Testing Your Agentic Step
The best way to test your agentic step is through Workbench. It lets you create pets, watch the AI enrichment happen in real-time, and see all the logs in one place.
Create a Pet
Open Workbench and test the CreatePet endpoint. The AI will automatically start enriching the profile in the background.
Prefer using curl?
Check the logs in Workbench to see the agentic step in action:

You'll see:
- "Pet created" log from the API endpoint
- "AI Profile Enrichment started" log
- "AI Profile Enrichment completed" with generated content
View the Enriched Profile
Fetch the pet in Workbench to see the AI-generated profile, or use curl:
Using curl?
You'll get back something like:

Building a Decision-Making Agentic Step
Now let's create an agentic step that doesn't just generate content - it makes decisions that control the workflow. This is called agentic routing.
The Health Review Agentic Step
This agentic step analyzes pet symptoms and decides if treatment is needed. Instead of you writing complex if-else logic, the AI evaluates the context and chooses the appropriate action.
View on GitHub:
How Decision-Making Agentic Steps Work
This agentic step is fundamentally different from the enrichment agentic step:
- It's an API Step - Staff trigger it explicitly when they need a decision
- It defines an emits registry - Lists all possible actions the AI can choose from (in
agent-decision-framework.ts/js) - It calls the AI with context + options - The AI evaluates and picks one
- It fires the chosen emit - This emit goes to the orchestrator, changing workflow state
- It uses idempotency checking - Caches recent decisions to prevent duplicate AI calls
The framework functions (buildAgentContext, callAgentDecision, getAgentArtifacts) handle the OpenAI call and ensure the AI picks from valid options.
Testing the Health Review Agentic Step
The best way to test decision-making agentic steps is through Workbench. You can create pets, trigger the health review, and watch the AI make decisions in real-time.
Create a Pet
Use Workbench to create a pet. The AI enrichment will automatically trigger.
Prefer using curl?
Trigger the Health Review
In Workbench, test the health review endpoint to see the AI make a decision.
Using curl?

You'll get a response like:

The AI evaluates the pet's data and makes a decision. The emit it fires will trigger the orchestrator to handle the appropriate state transition.
Verify the Status Change
Check the pet status in Workbench to see the AI's decision reflected in the workflow state.
Using curl?

The pet's status has automatically changed based on the AI's decision!
Connecting Agentic Steps to the Orchestrator
The real power comes when your agentic steps integrate with a workflow orchestrator. The orchestrator subscribes to the events emitted by agentic steps and handles the actual state transitions.
The orchestrator configuration shows it subscribes to agentic step events:
🎉 Congratulations! You've built intelligent agentic workflows that make decisions and drive workflows. Your pet shelter now has automated intelligence that would have taken hundreds of lines of complex logic to implement manually.
What's Next?
Your pet shelter now has intelligent agentic workflows making decisions! But how do you give users real-time feedback while all this AI processing happens in the background?
In the final guide, we'll add Real-Time Streaming to provide live updates as your workflows execute:
- Stream Configuration - Define stream schemas for type-safe updates
- API with Streaming - Initialize streams and return immediately to clients
- Background Job Streaming - Push real-time progress updates as jobs process
- Agentic Step Streaming - Stream AI enrichment progress in real-time
- Multi-Step Streaming - Multiple steps updating the same stream
Let's complete your system by adding real-time streaming capabilities!
Explore more examples in the Motia Examples Repository.
