Docs/Core Runtime·4 min read
Playground API & LLM Wiring
Connect the interactive hero playground to live OpenAI, Anthropic, or Gemini models using server-side streaming.
Built-in Route Handler
Out of the box, the hero playground runs ultra-snappy deterministic traces client-side so visitors never hit rate limits or leaked keys. When you want visitors to test live prompts against your backend model, point requests at POST /api/playground.
src/app/api/playground/route.ts
import { NextResponse } from "next/server";
export async function POST(req: Request) {
const { prompt, model, temperature } = await req.json();
if (!prompt || typeof prompt !== "string") {
return NextResponse.json({ error: "Prompt required" }, { status: 400 });
}
// Swap in process.env.OPENAI_API_KEY or ANTHROPIC_API_KEY here
return NextResponse.json({
model,
temperature,
output: `Processed safely for ${model}`,
usage: { tokens: 342, latencyMs: 280 },
});
}Protecting Public Playground Endpoints
Before exposing a live LLM API key on your public marketing landing page, apply strict payload limits and IP rate throttling.
Environment Variables
Never prefix LLM keys with NEXT_PUBLIC_. Store OPENAI_API_KEY or ANTHROPIC_API_KEY strictly in .env.local or Vercel project environment settings.
- Cap incoming prompts at 500 characters and max_tokens at 450.
- Enforce a per-IP sliding window (e.g., 10 runs/hour via Upstash Redis).
- Keep simulated trace fallbacks enabled if upstream latency spikes.