Duck
Tutorials9 min read

A Practical Tutorial on Deploying AI Agents That Don't Break

Samet Turan— Editor··9 min read

Stop building toy AI agents that fail on real data. This tutorial on deploying AI agents covers state management, error handling, and the exact no-code stack to ship a production-ready automation.

A Practical Tutorial on Deploying AI Agents That Don’t Break

You’ve seen the demos. An AI agent books a flight or summarizes a sales call. It looks simple. But when you try to build one for a real business process, it falls apart. This isn’t a guide to building another fragile demo; this is a tutorial on deploying AI agents that can handle the messy reality of production work.

What Most Guides Get Wrong About AI Agents

Most tutorials focus entirely on the prompt. They show you a clever prompt that chains a few thoughts together, and they call it an agent. This is a mistake. The prompt is the agent’s ‘brain,’ but a brain is useless without a body, a nervous system, and a memory. The infrastructure around the LLM is what makes an agent work, not just the prompt itself.

These guides often assume a single, perfect run. They don’t account for APIs failing, LLMs spitting out garbage, or the context changing mid-task. They build a glass cannon. Real work requires resilience. You need error handling, retries, and a clear record of what the agent did and why.

Finally, they often point you to consumer-grade tools. Stringing together a few steps in a basic Zapier plan might work for a personal notification, but it’s a terrible foundation for a core business process. The task limits are low, the pricing gets absurdly high with any real volume, and the debugging tools are primitive. You’re building on sand.

The Operator’s Stack: My Go-To AI Agent Framework

To build a durable agent, you need to think in layers. This isn’t just about connecting two apps; it’s about building a small, specialized system. Here’s the stack I use for almost every agent I deploy.

🤖
Recommended Reading

AI Side Hustles

12 Ways to Earn with AI

Practical setups for building real income streams with AI tools. No coding needed. 12 tested models with real numbers.


Get the Guide → $14

★★★★★ (89)

  • Orchestrator: This is the heart of the system. It’s not just a trigger-action tool; it’s a state machine that manages the flow of data and logic. My go-to is Make (formerly Integromat). Its visual builder is far better for complex, multi-path scenarios than linear tools. Honestly, its approach to error handling and data stores makes it a proper no-code ai development environment.
  • LLM Core: This is the brain. The OpenAI API (specifically GPT-4o) is my default for complex reasoning, but Anthropic’s Claude 3 Sonnet is a fantastic, cheaper alternative for tasks that are more about summarization or classification.
  • State Memory: The agent needs to remember what it has done. A simple Airtable base or even Make.com’s built-in Data Stores work perfectly for this. It tracks the job ID, current status, attempts, and any relevant data collected.
  • Tool Belt: These are the APIs the agent can use to interact with the world. This could be sending an email via Postmark, looking up customer data in a CRM, or using a tool like ScrapingBee to get data from a website.
  • Logging: This is non-negotiable. Every significant step, every API call, and every LLM response must be logged. Again, a simple Airtable base is a great place to start. When something breaks, this log is the first place you’ll look.

This structure separates the components, making the system easier to debug and upgrade. You can swap out the LLM or add a new tool without rebuilding the entire thing.

Building the ‘Brain’: A Lead Qualification Agent Example

Let’s make this concrete. Imagine a new lead fills out a form on your website. You want an agent to research the lead’s company, decide if they’re a good fit, and then route them accordingly. The core of this is the master prompt that guides the LLM.

It’s not just a simple question. It’s a detailed system prompt that defines the agent’s role, its available tools, the context it has, and—most critically—the exact format for its response. Forcing a JSON output is the single most important technique for making LLM outputs reliable enough for automation.

Here’s a simplified version of a prompt I’ve used:

You are an expert B2B lead qualification assistant. Your goal is to analyze a new lead and decide the next step.

## CONTEXT

You will be given JSON data for a new lead containing their name, email, and company website.

## AVAILABLE TOOLS

1. `research_company(website)`: This function will be executed by the system to get information about the company (employee count, industry, description).

2. `route_to_sales(reason)`: This function will assign the lead to the sales team.

3. `mark_as_unqualified(reason)`: This function will disqualify the lead.

## QUALIFICATION CRITERIA

- The company must be in the 'Software' or 'Manufacturing' industry.

- The company must have more than 50 employees.

## INSTRUCTIONS

1. First, analyze the provided lead data.

2. Decide if you have enough information. If not, your first action should be to use the `research_company` tool.

3. Once you have the company information, evaluate it against the qualification criteria.

4. Based on your evaluation, decide which function to call next: `route_to_sales` or `mark_as_unqualified`.

5. You MUST respond in a valid JSON format. Do not include any other text or explanation outside of the JSON object. Your response must contain two keys: `decision` (a string explaining your reasoning) and `next_action` (a JSON object with the function call).

## EXAMPLE RESPONSE

{ "decision": "The company has 200 employees and is in the software industry, meeting our criteria.", "next_action": { "function": "route_to_sales", "params": { "reason": "Qualified: 200 employees, Software industry." } } }

This prompt doesn’t leave much room for error. It tells the model exactly what to do and how to report back. This is the foundation of a reliable ai agent framework.

How Do You Handle State and Prevent Infinite Loops?

This is where the orchestrator and state memory become critical. An agent that forgets what it just did is useless. When our Make.com scenario receives a new lead, the very first step isn’t to call the LLM. The first step is to create a new record in our Airtable ‘Jobs’ base with a unique ID and a status of ‘new’.

Every subsequent step in the workflow updates this record. After calling the research tool, the status changes to ‘research_complete’. After the LLM makes its decision, the status becomes ‘qualified’ or ‘unqualified’. This way, if the workflow fails halfway through, you know exactly where it left off. You can even build a separate recovery scenario that periodically checks for stalled jobs.

My biggest gripe with many no-code platforms is their opaque state management. You think a workflow is done, but it actually timed out, and you have no idea where it stopped. Make.com’s visual execution history is a lifesaver here; you can literally see the data flow and pinpoint the exact module that failed.

To prevent infinite loops, we use a simple counter. Add a field to your Airtable job record called ‘attempts’. Increment it every time the agent retries a specific step. Set a hard limit. If the agent has tried to research the same company 3 times and the API keeps failing, the rule should be: stop, change the status to ‘error’, and send a notification to a human. Don’t let the robots run unsupervised forever.

The ‘Body’: Connecting Tools and Actions in Make.com

Let’s walk through the Make.com scenario for our lead qualifier.

  1. Webhook Trigger: The scenario starts when it receives data from your website form.
  2. Create Airtable Record: It immediately creates a new row in the ‘Jobs’ table, saving the lead data and setting `Status: new`, `Attempts: 0`.
  3. HTTP Request to OpenAI: It sends the master prompt and the lead’s data to the GPT-4o API.
  4. Parse JSON: Make.com has a built-in module to parse the text response from the LLM into a usable data structure.
  5. Router: This is the magic. A Router module creates conditional paths based on the LLM’s JSON output. We’ll set up a path for each possible `next_action.function` from our prompt.
  6. Conditional Paths:
    • Path 1 (If `function == route_to_sales`): This path uses the Postmark module to send a templated email to the sales team with the lead’s info and the qualification reason. Then, it updates the Airtable record’s `Status` to ‘routed’.
    • Path 2 (If `function == mark_as_unqualified`): This path simply updates the Airtable record’s `Status` to ‘unqualified’ and logs the reason. No further action needed.

The moment I got the router working based on the LLM’s JSON output was a huge win. It felt like I’d actually given the system decision-making power, not just a script to follow. It’s the core of a real ai automation blueprint.

How to Debug When This Inevitably Breaks

It will break.

Plan for it. The difference between a demo and a production system is how it behaves when things go wrong. Your logging table in Airtable is your best friend here. Log everything: the initial data, the exact prompt sent, the full raw response from the LLM, and the final action taken. When a lead gets misrouted, you can trace the entire decision-making process.

Here are the most common failure points I see:

  • Malformed JSON from the LLM: It happens, especially with simpler models. The best fix is to add an error-handling route in Make.com. If the ‘Parse JSON’ module fails, it routes the flow to a second OpenAI call. The prompt for this call is simple: “The following text is supposed to be valid JSON, but it is broken. Please fix it and return only the corrected JSON. Do not add any commentary. [Insert broken LLM response here]”. This works about 90% of the time.
  • External API Downtime: Your CRM’s API will go down. Your email service will have a blip. Wrap every single API call in an error-handling block. Make.com’s default is to stop the scenario, but you can configure it to retry a few times with an increasing delay (e.g., wait 1 minute, then 5, then 15).
  • Prompt Gaps: You’ll discover an edge case you didn’t plan for, and the agent will do something weird. This is a prompt engineering problem. When you find a failure, don’t just fix the one bad result. Update your master prompt to explicitly handle that category of edge case in the future. Version your prompts like code.

Building this way takes more time upfront. But it results in an asset, not a liability. You build it once, and it runs reliably in the background, doing valuable work. That’s the goal of any real operator playbook.

For more on this exact angle, deeper coverage of AI agent platforms.

If you’d rather skip the build and deploy a working version in an afternoon, we’ve packaged this entire workflow as a blueprint at deepusecase.com/vault/ai-agent-builder-kit.

— The Colophon

One AI tool. Tested. Reviewed.
In your inbox every Sunday.

~3 minute read. Real outcomes from operators, not marketers.

Free. One email per Sunday. Unsubscribe in one click.