Skip to content
© Devang Patel
AIFull-stack

Live AI Assistant

Chat that knows when to check the web, and cites what it found.

Role
Full-stack engineer (personal project)
Timeline
Feb 2026, built over 5 days
Team
Solo
Links
Code walkthrough on request

Stack:

  • Next.js 16
  • TypeScript
  • OpenAI GPT-4o
  • Groq
  • Tavily
  • Supabase
  • Vercel AI SDK

Overview

An AI assistant that streams answers token by token, searches the web when a question needs fresh information, and cites its sources inline. It runs on the Edge runtime and switches between OpenAI GPT-4o and Groq's Llama 3.3 70B with a single environment variable.

The problem

Standard chatbots are stuck with stale training data. This one should know when to reach for the live web, and answer with citations rather than confident guesses.

Constraints

  1. 01

    The first token has to arrive fast. A chat UI that waits for the whole answer feels broken.

  2. 02

    Search costs money and latency, so it should only run when the question needs it.

  3. 03

    There had to be a free path: the demo must work without a paid OpenAI key.

Architecture

  1. Client
    • Chat UIStreamed Markdown (react-markdown, GFM)
  2. Edge
    • /api/chatProvider switch, search router, prompt assembly, streaming
  3. Services
    • Groq or OpenAIllama-3.3-70b-versatile, or GPT-4o
    • TavilyWeb search results, numbered for citation
  4. Data
    • Supabase PostgresConversations and messages schema
The route decides whether to search before it calls the model, then streams the answer straight back.

Decisions and trade-offs

From tool-calling to a search router

Chose
A deterministic router that decides before the model call
Over
Letting GPT-4o call a search tool itself (the first version)
Why
Adding Groq as a free provider meant the search decision couldn't depend on one vendor's tool-calling behaviour. A router is cheaper, predictable and testable.
Trade-off
It misses questions that need fresh data but don't use the trigger words. A cheap classifier call would catch them.

The Edge runtime for chat

Chose
export const runtime = 'edge' on the chat route
Over
A Node serverless function
Why
Streaming starts close to the user, with no cold Node boot in front of the first token.
Trade-off
Node-only SDKs are off the table, which pinned the AI SDK to 3.4 and the OpenAI SDK to v4.

Citations by prompt contract

Chose
Search results injected into the system prompt as numbered sources
Over
A separate citation-matching step
Why
Fast and simple: the model cites [1], [2] as it writes.
Trade-off
Nothing verifies that a citation actually supports its sentence. That check is the next thing to build.

The hardest problem

Deciding when to search

A web search adds a full round trip before the first token, so it can't run on every message. The route makes the call up front, with no extra model request, then streams whatever comes back.

src/app/api/chat/route.ts (condensed)ts
export const runtime = "edge";

const shouldSearch = (query: string) =>
  [/latest/i, /current/i, /recent/i, /today/i, /news/i, /price of/i, /search for/i]
    .some((pattern) => pattern.test(query));

export async function POST(req: Request) {
  const { messages } = await req.json();
  const query = messages.at(-1)?.content ?? "";
  const { model, client } = getProviderAndModel(); // Groq if GROQ_API_KEY is set, else GPT-4o

  let system = getSystemPrompt();
  if (shouldSearch(query)) {
    const results = await searchWeb(query); // Tavily
    system = getSystemPrompt(formatSearchResults(results.results)); // numbered [1], [2]…
  }

  const response = await client.chat.completions.create({
    model, stream: true,
    messages: [{ role: "system", content: system }, ...messages],
  });
  return new StreamingTextResponse(OpenAIStream(response));
}

Error handling and the full keyword list omitted.

Results

Build time
5 days
15 to 19 Feb 2026
Providers
2, one switch
Groq Llama 3.3 70B or OpenAI GPT-4o

What I'd do next

  1. 01

    Replace keyword routing with a cheap classifier call, or tool-calling where the provider supports it, so implicit questions get searched too.

  2. 02

    Verify citations: check that each [n] actually supports the sentence it's attached to.

  3. 03

    Wire conversation history through the UI end to end. The Supabase schema and save/load helpers are written; the chat screen should use them.

Next project

Custom WordPress Themes

Themes built by hand from client designs. No page-builder bloat.

Read the case study