The last six weeks have transformed what developers can build. OpenAI released the ChatGPT API (gpt-3.5-turbo) on 1 March at a tenth of the price of previous models, and GPT-4 followed on 14 March with a large jump in reasoning quality. Teams everywhere are prototyping features: summarisation, classification, extraction, drafting and conversational interfaces.
Prototypes are easy. A prompt in the playground, a few impressive outputs, and a demo is ready by the afternoon. Production is harder, because production has to work on the thousandth input, not just the first five.
Step 1: separate the prompt from the code
Treat prompts as versioned templates, not strings scattered through the codebase:
SUMMARY_PROMPT_V3 = """You summarise customer support tickets for engineers.
Rules:
- Use at most 5 bullet points.
- Include product area, observed behaviour, expected behaviour, and any error codes.
- If information is missing, say "unknown". Do not guess.
Ticket:
\"\"\"
{ticket_text}
\"\"\"
"""Keep them in version control, review changes, and record which version produced each output.
Step 2: constrain and validate outputs
Free-form text is hard to use programmatically. Ask for a specific format, then validate it in code. At the moment, models do not guarantee valid JSON, so validation and retry logic are essential:
import json, openai
from pydantic import BaseModel, ValidationError
from tenacity import retry, stop_after_attempt, wait_exponential
class TicketTriage(BaseModel):
category: str # one of: billing, login, bug, feature_request, other
severity: int # 1-4
summary: str
@retry(stop=stop_after_attempt(3), wait=wait_exponential(min=1, max=10))
def triage(ticket_text: str) -> TicketTriage:
resp = openai.ChatCompletion.create(
model="gpt-4",
temperature=0,
messages=[
{"role": "system", "content": "Reply with JSON only: "
'{"category": ..., "severity": 1-4, "summary": ...}'},
{"role": "user", "content": ticket_text[:8000]},
],
request_timeout=30,
)
data = json.loads(resp["choices"][0]["message"]["content"])
result = TicketTriage(**data) # raises if malformed
if result.category not in {"billing", "login", "bug", "feature_request", "other"}:
raise ValueError("unexpected category")
return resultStep 3: build an evaluation set early
Before tuning prompts or comparing models, collect 50 to 200 real examples with expected outputs or grading criteria. Then every change can be measured:
- accuracy or agreement with human labels,
- format validity rate,
- hallucination rate on questions that should be answered "unknown",
- latency and cost per request.
Without this, prompt changes are guesswork: fixing one example often breaks three others.
Step 4: choose the right model per task
GPT-4 is more capable and considerably more expensive and slower than GPT-3.5 Turbo. Many production features use both:
- GPT-3.5 Turbo for high-volume, simpler tasks such as classification, short extraction and routing.
- GPT-4 for complex reasoning, nuanced writing and difficult cases, or as a fallback when the cheaper model's output fails validation.
Step 5: design for failure
flowchart LR
R[Request] --> C{Cache hit?}
C -->|yes| Out[Response]
C -->|no| M[Model call<br/>timeout + retries]
M --> V{Valid output?}
V -->|yes| Out
V -->|no| F[Fallback:<br/>stronger model,<br/>default, or human queue]
M -->|rate limited / down| F
F --> Out- Timeouts and retries with backoff: APIs are sometimes slow or unavailable.
- Rate limits: queue and throttle, especially for batch jobs.
- Fallbacks: a different model, a rules-based default or a human queue.
- Caching: identical requests are common and expensive.
- Graceful degradation: the product should still work, perhaps with reduced features, if the LLM is unavailable.
Step 6: manage cost and latency
- Trim prompts: remove unnecessary instructions and examples.
- Limit input size and
max_tokens. - Stream responses for user-facing text, so users see output immediately.
- Track cost per feature and per customer from day one.
Step 7: security and privacy
- Prompt injection: user input and documents can contain instructions. Do not let model output trigger sensitive actions without validation and authorisation in code.
- Output handling: encode model output before rendering it in HTML; never execute it.
- Data: understand the provider's data retention and usage terms; avoid sending unnecessary personal or confidential data.
- Abuse: rate-limit per user and monitor for misuse of your AI feature.
Step 8: operate it
Log inputs, outputs, prompt versions, latency and cost (with appropriate privacy controls). Sample outputs for human review. Watch for drift as the provider updates models: pin model versions where possible and re-run evaluations when they change.
The bottom line
GPT-4 makes it possible to build features that were unimaginable a year ago. Shipping them reliably still takes software engineering: versioned prompts, validated outputs, evaluation sets, failure handling, cost control and security. The teams treating LLM features as real software, rather than magic prompts, are the ones reaching production.
Our secure SDLC and agentic AI security teams build production LLM features with this discipline built in.