Skip to Content

Respond to customer inquiry and check for inappropriate content

Create an AI agent workflow that responds to customer inquiries while checking if their text is inappropriate

Overview

Parallelization is a workflow pattern where multiple tasks or processes run simultaneously instead of sequentially, allowing for more efficient use of resources and faster overall execution. It's particularly valuable when different parts of a task can be handled independently, such as running content analysis and response generation at the same time. Parallelization

Example task

In this example, we'll create a workflow that simultaneously checks content for issues while responding to customer inquiries. This approach is particularly effective when tasks require multiple perspectives or parallel processing streams, with the orchestrator synthesizing the results into a cohesive output.

This task:

  • Uses generateText from the AI SDK to call Anthropic's Claude models
  • Uses experimental_telemetry to surface each LLM call on the Run page in the dashboard
  • Uses batch.triggerByTaskAndWait to run customer response and content moderation tasks in parallel
  • Answers with claude-sonnet-5-5 and moderates with the faster, cheaper claude-haiku-5-5
  • Simultaneously checks for inappropriate content while generating responses
import {  } from "@ai-sdk/anthropic";
import { ,  } from "@trigger.dev/sdk";
import {  } from "ai";

// Task to generate customer response
export const generateCustomerResponse = ({
  : "generate-customer-response",
  : async (payload: { question: string }) => {
    const response = await ({
      : ("claude-sonnet-5-5"),
      : "You are a helpful customer service representative.",
      messages: [
        { role: "user", content: payload.question },
      ],
      : {
        : true,
        : "generate-customer-response",
      },
    });

    return response.;
  },
});

// Task to check for inappropriate content
export const checkInappropriateContent = ({
  : "check-inappropriate-content",
  : async (payload: { text: string }) => {
    const response = await ({
      : ("claude-haiku-5-5"),
      : "You are a content moderator. Respond with 'true' if the content is inappropriate or contains harmful, threatening, offensive, or explicit content, 'false' otherwise.",
      messages: [
        { role: "user", content: payload.text },
      ],
      : {
        : true,
        : "check-inappropriate-content",
      },
    });

    return response..().("true");
  },
});

// Main task that coordinates the parallel execution
export const handleCustomerQuestion = ({
  : "handle-customer-question",
  : async (payload: { question: string }) => {
    const {
      runs: [responseRun, moderationRun],
    } = await .triggerByTaskAndWait([
      {
        task: generateCustomerResponse,
        payload: { question: payload.question },
      },
      {
        task: checkInappropriateContent,
        payload: { text: payload.question },
      },
    ]);

    // Check moderation result first
    if (moderationRun.ok && moderationRun.output === true) {
      return {
        response:
          "I apologize, but I cannot process this request as it contains inappropriate content.",
        wasInappropriate: true,
      };
    }

    // Return the generated response if everything is ok
    if (responseRun.ok) {
      return {
        response: responseRun.output,
        wasInappropriate: false,
      };
    }

    // Handle any errors
    throw new Error("Failed to process customer question");
  },
});

Run a test

On the Test page in the dashboard, select the handle-customer-question task and include a payload like the following:

{
  "question": "Can you explain 2FA?"
}

When triggered with a question, the task simultaneously generates a response while checking for inappropriate content using two parallel LLM calls. The main task waits for both operations to complete before delivering the final response.

Was this page helpful?

Last updated

Edit on GitHub

On this page