Skip to content

Latest commit

Β 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 ChatLLM β€” AI Super Assistant with RouteLLM

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.


✨ Features

RouteLLM Engine

  • 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

Chat Mode

  • 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

Agents Mode (5 specialized agents)

  • πŸ”¬ 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).

Stats Bar

Live counters: Haiku / Sonnet / Opus usage, total routed messages, estimated savings vs Opus, average complexity score.


πŸš€ Quick Start

1. Clone & install

git clone <your-repo>
cd chatllm
npm run install:all

2. Configure environment

cp server/.env.example server/.env
# Edit server/.env and add your ANTHROPIC_API_KEY

3. Run in development

npm run dev

4. Build for production

npm run build    # Builds React into server/public/
npm start        # Serves everything from Express

πŸ“ Project Structure

chatllm/
β”œβ”€β”€ 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

πŸ›  API Reference

POST /api/chat

Stream a chat completion with RouteLLM routing.

Body:

{
  "messages": [{ "role": "user", "content": "..." }],
  "forceModel": null,
  "stream": true
}

SSE Events: routing, token, done, error


POST /api/agent/:id/chat

Stream an agent response with step-by-step reasoning events.

Agents: researcher, coder, writer, analyst, planner

SSE Events: agent, routing, step, token, done, error


POST /api/route/analyze

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
}

GET /api/route/models

Returns all model definitions and routing thresholds.


βš™οΈ RouteLLM 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

πŸ”‘ Environment Variables

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

πŸ“¦ Tech Stack

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages