Interactive Code Walkthrough

How a robot names a price — and who checks its work.

When a recycler lists EPR plastic credits on the Novus exchange, an AI suggests what to charge. But this endpoint never takes that number on faith. Scroll to watch one request travel from the click to the final rupee — and try the pricing desk yourself.

🧠 generateObject · Vercel AI SDK 🔒 Clerk auth + rate limit 🗄️ Supabase live market 🤖 claude-sonnet-4-6
The pipeline

One POST request, eight checkpoints

Every stage runs in order. The cheap safety checks come first, so a bad request never reaches the expensive AI call. Hover any stage.

01🔒Auth gateClerk user or seller session — else 401.
02⏱️Rate limitCalling too fast? 429.
03ValidateZod checks category + qty — else 400.
04🗄️Read marketActive Supabase listings for this category.
05✍️Build promptFloor + average → plain-English briefing.
06🧠generateObjectAI returns a typed price object.
07🛟ClampPull the price into a safe band.
08📤RespondReturn price, speed, reason as JSON.
Try it · the pricing desk

Watch the guardrail catch a rogue price

Pick a plastic category, look at the live market the code would read from Supabase, then ask the AI for a price. Flip “let the AI go rogue” to see what happens when the model returns something wild — and how the clamp reins it in.

pricing-desk · demo market live

Active listings (Supabase)

    Market floor (min)
    Average price
    Safe band the code allows
    Let the AI go rogue
    floor average safe band [lo, hi] AI raw suggestion final price
    Why the clamp exists

    The AI proposes. The code disposes.

    ✗ Trust the number blindly

    Return whatever the model says. One hallucinated answer — ₹0, or ₹9,999/kg — flows straight to a real seller and a real buyer. A single bad token becomes a real financial mistake on a live marketplace.

    ✓ Clamp to the live market

    Bound the AI’s number to a band derived from the real floor and average before anyone sees it. The model still guides the price within reason, but it can never push a trade off a cliff.

    The two ideas that make it safe

    Structured output, then a guardrail

    First, the AI is forced to answer in a fixed shape — no fragile text parsing. Second, the one value that touches money is clamped. Here is the real code.

    structured output
    // Force the model to fill an exact shape, not write prose
    const suggestionSchema = z.object({
      suggested_price_per_kg: z.number(),
      sell_speed: z.enum(['fast', 'moderate', 'slow']),
      reasoning: z.string(),
    })
    
    const { object } = await generateObject({
      model, schema: suggestionSchema, system, prompt,
    })
    the guardrail
    // Never trust output blindly — clamp to a sane band around the market.
    const lo = Math.max(0.01, floor * 0.5)
    const hi = Math.max(avg * 2, floor * 1.5, 1)
    const price = Math.min(hi, Math.max(lo, object.suggested_price_per_kg))
    The core lesson
    Schema validation guarantees the shape of the answer — not that the value is sane. So the code adds one more line of defense: it clamps the price to a band built from real market data. That guardrail is what makes it safe to put an LLM in the money path.
    In one breath

    The whole thing, recapped