how to verify response generated by ai in api response quickly
13:28 05 Nov 2026

How should I validate AI-generated workout responses and handle timeouts in a Node.js application?

I am developing FitOS, a fitness application built with Node.js, Express, TypeScript, and PostgreSQL. It uses OpenRouter to generate personalized workout recommendations.

The application has multiple AI-powered features:

  • Workout generation based on target muscles and available gym equipment.
  • Exercise substitutions.
  • Exercise recommendations and explanations.
  • Food recommendations and nutrition analysis.

I'm facing reliability issues when multiple AI operations are involved.

Problems I'm experiencing

  1. Invalid AI responses: The AI sometimes returns exercises that don't match the requested muscle group or available equipment.
  2. Duplicate recommendations: Workout generation occasionally returns multiple exercises targeting almost identical movement patterns.
  3. Latency: Some AI requests take several seconds, making the application feel slow.
  4. Timeouts and provider failures: OpenRouter requests can time out or return HTTP errors.
  5. Fallback handling: I want the application to use existing exercise catalogue data when AI-generated results fail validation or the provider becomes unavailable.

Current implementation

I am using OpenRouter's Chat Completions API:

const response = await fetch(
  "https://openrouter.ai/api/v1/chat/completions",
  {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: process.env.OPENROUTER_MODEL,
      temperature: 0.1,
      messages: [
        {
          role: "system",
          content: "Generate workout exercises using only the available equipment."
        },
        {
          role: "user",
          content: JSON.stringify({
            focus: "Shoulders",
            availableEquipment: ["Dumbbells", "Cable Machine"],
            requestedCount: 3
          })
        }
      ]
    }),
    signal: AbortSignal.timeout(10000)
  }
);

I am also using Zod to validate the returned JSON structure and additional business rules to check target muscles and available equipment.

For example, my expected response structure is:

const WorkoutSchema = z.object({
  exercises: z.array(
    z.object({
      exercise: z.string(),
      muscle: z.string(),
      machine: z.string(),
      sets: z.number().int().min(1).max(6),
      reps: z.number().int().min(1).max(30),
    })
  ).min(1)
});

What I want to achieve

I want to implement a reliable AI service architecture where:

  • AI responses are validated using schemas and deterministic business rules.
  • Invalid or duplicate exercises are rejected.
  • Failed AI operations fall back to trusted database/catalogue recommendations.
  • Requests have strict timeouts and bounded retries.
  • Multiple AI operations do not unnecessarily block each other.
  • Provider failures are handled without breaking the main application.
  • Latency, provider failures, and validation errors are monitored.

My questions

  1. What is the recommended architecture for managing multiple AI operations in a Node.js/Express backend?
  2. Is Zod validation combined with deterministic business rules sufficient, or should I use another verification layer?
  3. What is the best approach for handling provider timeouts, retries, and fallback providers?
  4. How can I prevent duplicate or semantically similar exercises in generated workout plans?
  5. How should I monitor latency and reliability when multiple AI operations run concurrently?

I am particularly interested in practical implementation patterns rather than just theoretical recommendations.

Tech stack: Node.js 22, Express, TypeScript, PostgreSQL, OpenRouter, Zod, and Redis.

artificial-intelligence