AI in Production: What No One Tells You

Hallucinations, latency, cost explosion, RAG pitfalls, and the operational realities of shipping LLM features to real users.

By Rais Helmy, IT consultant in Kuala Lumpur, Malaysia.

AI in Production. The hard parts no one mentions.

The demo worked. Production is a different story. Hallucinations, 4-second latencies, $400 API bills, and RAG that retrieves the wrong context.

reality 1 · hallucinations & guardrails

Expectation: best practice

  • Validate LLM output structurally — parse JSON, check required fields, reject malformed responses.
  • Use deterministic tools for facts: dates, prices, DB lookups. LLM adds intelligence, not data.
  • Add output filters: profanity, PII leakage, off-topic detection.

Reality: what most teams do

  • Trusting the model's answer as ground truth — users get wrong data served confidently.
  • No retry/fallback on malformed JSON output — app crashes with parse errors.
  • Sending customer PII straight into OpenAI without reviewing data handling agreements.

Example (code)

// ✦ Validate + fallback LLM output
async function classifyIntent(text) {
  const raw = await llm.complete({
    prompt: `Classify as JSON: {intent, confidence}
"${text}"`
  });

  let parsed;
  try { parsed = JSON.parse(raw); }
  catch { return { intent: 'unknown', confidence: 0 }; }

  if (!VALID_INTENTS.includes(parsed.intent)) {
    return { intent: 'unknown', confidence: 0 };
  }
  return parsed;
}

LLM latency and cost scale faster than you expect.

A single GPT-4 call at 2,000 input tokens + 500 output tokens costs ~$0.07. At 100k calls/day that's $7,000/day. Response time averages 3–5 seconds. Stream responses and cache aggressively.

Cost killers

Sending entire conversation history · No input token limits · Using GPT-4 for tasks GPT-3.5 handles · Re-computing identical prompts

Cost fixes

Semantic caching (exact + near-duplicate) · Summarise history > 4k tokens · Route simple tasks to smaller models · Stream responses for UX

→ Add cost tracking from day one: log (model, input_tokens, output_tokens, user_id) per call — or your AI feature will kill your margins silently.

reality 3 · rag pitfalls

Expectation: best practice

  • Chunk documents semantically (by section/paragraph), not by fixed token count.
  • Embed the question AND retrieved chunks together in the final prompt for coherence.
  • Evaluate retrieval quality separately from generation quality.

Reality: what most teams do

  • Splitting PDFs every 500 chars — cuts sentences mid-thought, retrieves incoherent fragments.
  • Retrieving top-5 chunks but injecting all 5 even when 3 are irrelevant — adds noise.
  • "RAG is set up, AI is done" — retrieval degrades as source documents grow and no one monitors it.

Example (code)

// ✦ Score and filter retrieved chunks
const chunks = await vectorStore.query({
  embedding: await embed(userQuery),
  topK: 8
});

// Filter low-relevance chunks
const relevant = chunks.filter(c => c.score > 0.78).slice(0, 4);

if (relevant.length === 0) {
  return "I don't have enough context to answer that.";
}

const context = relevant.map(c => c.text).join('
---
');
return await llm.complete(buildPrompt(userQuery, context));

ai feature production checklist

  • Output validation: Parse + schema check - Reject malformed responses, fallback gracefully
  • Cost tracking: Log every call - model + tokens + user — alert on anomalies
  • Latency: Stream + cache - Stream for UX, semantic cache for cost
  • PII / Safety: Filter before & after - Strip PII in, block harmful content out
  • Eval pipeline: LLM-as-judge - Automated quality scoring on sample outputs
  • No skip these: Rate limits - Per-user quotas — one runaway prompt destroys budget