STACK
Ch 19 · AI Engineering & LLM Patterns
Chapter 19 · The new layer

AI Engineering — LLMs in the dev workflow and in your product

AI is both a developer tool and a platform layer in your product. Two halves, one chapter — with deeper coverage in the companion AI Codex.

19.1 · AI in the dev workflow

ToolModeBest at
GitHub CopilotIDE completion + chat + agentInline suggestions; deep IDE integration.
CursorAI-first editorMulti-file edits; repo-aware chat.
Claude CodeTerminal agentPlan / explore / edit across repos with MCP + sub-agents.
Continue / Cline / AiderOSS CLI/IDE agentsBring-your-own-model; local options.
Sourcegraph CodyEnterprise code search + AIRepo-graph context.
Codeium / Tabnine / WindsurfSelf-host / VPCPrivacy-sensitive enterprises.
CodeRabbit / Greptile / KorbitAI code reviewPR-level review bot.
v0 / Bolt / LovableGenerative app scaffoldingFull UI from a prompt.

19.2 · Reviewing AI-generated code

  • Hallucinated APIs — verify imports exist.
  • Silent try/except swallow — easy to ship; hard to debug.
  • Fabricated dependencies — typo-squatting risk.
  • Commented-out failing tests instead of fixing them.
  • Over-engineering — push for the simplest change.

19.3 · The LLM app stack

User / agent→ AI gateway (LiteLLM / Portkey / Kong AI)→ Model providers (Claude / GPT / Gemini / Llama)
Vector DB (pgvector / Qdrant) Document store MCP servers (tools / resources) Eval & observability (LangFuse / OpenLLMetry)

19.4 · LLM application patterns

PatternWhen
Prompt onlyOne-shot generation; classification.
Few-shotPattern from 2–5 examples.
RAGAnswer over a fresh / private knowledge base.
Function calling / toolsThe model must take actions or fetch data.
Agent (ReAct loop)Multi-step planning + tool use.
Multi-agentSpecialist teams (researcher + writer + critic).
Reasoning modelHard math / code / planning — let the model think longer.
Fine-tune (LoRA)Style / behaviour shift the prompt can't achieve.

19.5 · RAG essentials

Docs→ Chunk + embed→ Vector DB→ Query → retrieve → rerank→ LLM answer + citations
  • Default: hybrid (BM25 + vector) with reciprocal-rank fusion.
  • Re-ranker (Cohere / Voyage / BGE) for top-k precision.
  • Advanced: CRAG, RAPTOR, GraphRAG, ColPali for multimodal PDFs.
  • Evaluate with Ragas / TruLens / DeepEval — context recall / precision / faithfulness / answer relevance.

19.6 · Agents — the loop

Goal→ Think→ Act (tool)→ Observe→ Reflect→ Done?

Frontier patterns: computer-use agents, browser agents, voice agents, multi-agent teams, agent-to-agent (A2A) handoffs. Frameworks: LangGraph, CrewAI, Claude Agent SDK, OpenAI Agents SDK, AutoGen.

19.7 · MCP & A2A

  • MCP (Model Context Protocol): standard JSON-RPC interface for exposing tools, resources, prompts to LLM hosts. Becoming universal for AI integrations.
  • A2A (Agent-to-Agent): protocol for agents from different vendors to advertise capabilities, exchange tasks, stream updates.

19.8 · AI gateways

GatewayNotes
LiteLLMOSS proxy translating 100+ provider APIs into OpenAI's format.
PortkeyManaged gateway with fallback, semantic cache, observability.
Kong AI GatewayEnterprise; AI plugins on Kong.
OpenRouterPay-per-token aggregator; great for prototyping.

19.9 · LLM observability

  • OpenLLMetry + OTel GenAI semantic conventions — vendor-neutral.
  • LangFuse, LangSmith, Arize Phoenix, Helicone — managed.
  • Capture per call: prompt hash, tokens in/out, latency, cost, tool calls, user, tenant.
  • Tag with eval scores; build trace-replay tooling for incidents.

19.10 · Cost & latency

  • Prompt caching — keep stable prefix at the top → 50–90% prefill savings.
  • Model cascade — try small/cheap first; fall back to frontier on low confidence.
  • Batching — provider batch API for ~50% discount on offline jobs.
  • Streaming — perceived latency drops dramatically.
  • Per-tenant budgets & token caps at the gateway.

19.11 · Safety & evals

  • Frozen eval suite (20–200 tasks); LLM-as-judge + rubric.
  • OWASP LLM Top-10 (2025) — prompt injection, sensitive info disclosure, excessive agency.
  • Guardrails: NeMo Guardrails, Guardrails AI, LLM Guard, Llama Guard.
  • Compliance: NIST AI RMF + 600-1 GenAI Profile, EU AI Act, ISO/IEC 42001 (full coverage in the AI Codex).

19.12 · Self-check

Q1 · The single highest-leverage cost optimisation for LLM apps?
Switching providers monthly
Prompt caching with stable prefixes
Bigger model
Higher temperature