A full-stack chat application with intelligent RouteLLM model routing for both Chat and Agents modes. Automatically selects the optimal Claude model (Haiku, Sonnet, or Opus) based on query complexity β reducing cost while maintaining quality.
- Auto-routing β scores each query 0β100 across 7 dimensions (length, code depth, reasoning, math/science, planning, creativity, simplicity)
- Three tiers β β‘ Haiku (fast/cheap), βοΈ Sonnet (balanced), π Opus (most capable)
- Live preview β shows predicted model as you type, updates in real time
- Route badge on every AI message with complexity bar and score
- Decision callout β explains why a model was selected
- Cost savings tracker β estimates savings vs always using Opus
- Manual override β turn off RouteLLM to force a specific model
- Full conversation history with localStorage persistence
- SSE streaming with real-time token delivery
- Multi-turn context maintained across messages
- Regenerate any response
- Copy individual messages or full chat
- π¬ Researcher β deep analysis, citations, structured summaries
- π» Code Assistant β write, debug, review & explain code
- βοΈ Writer β creative & professional writing
- π Data Analyst β data insights & interpretation
- π Task Planner β goals β actionable steps
Each agent has a specialized system prompt. RouteLLM runs for every agent request with a 3-step reasoning panel (analyze β route β invoke).
Live counters: Haiku / Sonnet / Opus usage, total routed messages, estimated savings vs Opus, average complexity score.
git clone <your-repo>
cd chatllm
npm run install:allcp server/.env.example server/.env
# Edit server/.env and add your ANTHROPIC_API_KEYnpm run dev- Frontend: http://localhost:5173
- Backend API: http://localhost:3001
npm run build # Builds React into server/public/
npm start # Serves everything from Expresschatllm/
βββ package.json # Root workspace (scripts + concurrently)
β
βββ server/
β βββ package.json
β βββ .env.example
β βββ src/
β βββ index.js # Express server
β βββ routellm.js # RouteLLM scoring & routing engine
β βββ agents.js # Agent definitions & message builder
β βββ middleware/
β β βββ auth.js # API key validation
β βββ routes/
β βββ chat.js # POST /api/chat (SSE streaming)
β βββ agent.js # POST /api/agent/:id/chat (SSE + steps)
β βββ route.js # POST /api/route/analyze
β
βββ client/
βββ package.json
βββ vite.config.js
βββ index.html
βββ src/
βββ main.jsx # React entry
βββ App.jsx # Root component + send logic
βββ store/
β βββ index.js # Zustand store (convs, agents, stats)
βββ hooks/
β βββ useRouteLLM.js # SSE streaming hooks
βββ components/
β βββ Sidebar.jsx # Navigation (Chat + Agents)
β βββ Topbar.jsx # Title bar + actions
β βββ StatsBar.jsx # Live RouteLLM stats
β βββ Message.jsx # Message row with route badge
β βββ InputBar.jsx # Textarea + route controls
β βββ EmptyState.jsx # Empty state + suggestions
β βββ RouteUI.jsx # RouteBadge, RouteDecision, AgentSteps
βββ styles/
β βββ globals.css
βββ utils/
βββ index.js
Stream a chat completion with RouteLLM routing.
Body:
{
"messages": [{ "role": "user", "content": "..." }],
"forceModel": null,
"stream": true
}SSE Events: routing, token, done, error
Stream an agent response with step-by-step reasoning events.
Agents: researcher, coder, writer, analyst, planner
SSE Events: agent, routing, step, token, done, error
Analyze query complexity without calling Anthropic.
Body: { "text": "..." }
Response:
{
"score": 72,
"modelKey": "opus",
"model": { "id": "...", "label": "Opus 4", "costPer1k": 0.015 },
"reason": "High complexity query β most capable model selected",
"isManual": false
}Returns all model definitions and routing thresholds.
| Score | Model | Use case |
|---|---|---|
| 0β34 | β‘ Haiku 4.5 | Simple lookups, short questions |
| 35β67 | βοΈ Sonnet 4 | Technical questions, moderate reasoning |
| 68β100 | π Opus 4 | Deep analysis, complex multi-step reasoning |
| Variable | Description | Default |
|---|---|---|
ANTHROPIC_API_KEY |
Your Anthropic API key | required |
PORT |
Server port | 3001 |
CLIENT_URL |
CORS origin | http://localhost:5173 |
NODE_ENV |
Environment | development |
| Layer | Tech |
|---|---|
| Frontend | React 18 + Vite |
| State | Zustand (persisted) |
| Backend | Express.js |
| AI SDK | @anthropic-ai/sdk |
| Streaming | Server-Sent Events (SSE) |
| Markdown | marked.js |
| Security | helmet, rate-limiting, CORS |