Back to blog
June 15, 2026·7 min read

Integrating the Claude API Into Production: Structured Output & Retries

AI
LLM
Claude
Backend

Wiring an LLM into a backend looks trivial in a tutorial: send a prompt, get text back. Production is where it gets interesting. The model occasionally returns malformed output, sometimes it is slow, and once in a while the call just fails. If your code assumes the happy path, those edges become incidents. The fix is to treat the model like any other unreliable external dependency.

The request flow

Validateinput + rate limitAssembleprompt + contextClaude APItimeout · retriestool / JSON modeParsevalidate schemaDelivertyped resultFallbackcached / default / queueon error / timeout

Validate → assemble → call with timeout + retries → parse against a schema → deliver a typed result, with a fallback path on failure.

Get structured output, not prose

If you need data back, do not parse free-form text with regex. Ask the model for a specific shape and validate it. With Claude, tool use (function calling) reliably constrains the output to a schema you define — and you reject anything that does not match instead of hoping it is right.

typescriptconst result = await client.messages.create({
  model: 'claude-opus-4-8',
  max_tokens: 1024,
  tools: [{
    name: 'extract_requirements',
    description: 'Extract the required skills from a free-text job description',
    input_schema: {
      type: 'object',
      properties: { skills: { type: 'array', items: { type: 'string' } } },
      required: ['skills'],
    },
  }],
  tool_choice: { type: 'tool', name: 'extract_requirements' },
  messages: [{ role: 'user', content: description }],
});

// Validate before you trust it — never assume the shape is correct.
const parsed = RequirementsSchema.parse(getToolInput(result));

Challenge

Timeouts and retries

Model latency is variable. Wrap every call in a timeout and retry transient failures with exponential backoff and jitter — but cap it. A request that has already waited 20 seconds should fail over to a fallback, not retry forever and pile up.

Always have a fallback

The model will be unavailable at some point. Decide ahead of time what happens then: serve a cached result, fall back to a deterministic default, or queue the work for later. The one unacceptable answer is an unhandled 500 in the user’s face.

Note

Use the LLM only where language matters

The strongest systems keep deterministic logic deterministic and reserve the model for what it is uniquely good at: turning unstructured language into structure, and structure into natural language. Do not put a probabilistic model on a path that simple math handles better.

Takeaways

Building an AI-powered backend or migrating an LLM prototype to production? Explore our AI / LLM integration and backend services or describe your project in the Project Builder for a tailored architecture plan.


Working on something like this?

I build NestJS backends and AI integrations for SaaS, fintech and crypto products. Happy to talk through your architecture.

Get in touch →