Day 2 of 5
⏱ ~60 minutes
Build an AI Chatbot — Day 2

Backend: Conversation History and System Prompts

A real chatbot remembers what was said earlier in the conversation. Day 2 adds conversation history and a configurable system prompt to your backend.

The Conversation History Problem

Yesterday's server forgets every exchange immediately. The Claude API is stateless — you must send the full conversation history with each request. Today you will build that.

server.js — Updated with History
const express = require('express');
const Anthropic = require('@anthropic-ai/sdk');
const cors = require('cors');
require('dotenv').config();

const app = express();
const client = new Anthropic();

app.use(express.json());
app.use(cors());
app.use(express.static('.'));

// In-memory history (per server session)
const conversations = {};

app.post('/chat', async (req, res) => {
  const { message, sessionId } = req.body;
  
  if (!conversations[sessionId]) {
    conversations[sessionId] = [];
  }
  
  // Add user message to history
  conversations[sessionId].push({
    role: 'user',
    content: message
  });
  
  const response = await client.messages.create({
    model: 'claude-opus-4-5',
    max_tokens: 1024,
    system: 'You are a helpful assistant. Be concise and friendly.',
    messages: conversations[sessionId]
  });
  
  const reply = response.content[0].text;
  
  // Add assistant response to history
  conversations[sessionId].push({
    role: 'assistant',
    content: reply
  });
  
  res.json({ reply });
});

app.listen(3000, () => console.log('Running on port 3000'));

Testing Multi-Turn Conversation

Test Multi-Turn
# First message
curl -X POST http://localhost:3000/chat   -H "Content-Type: application/json"   -d '{"message":"My name is Alex", "sessionId":"user1"}'

# Second message — Claude should remember the name
curl -X POST http://localhost:3000/chat   -H "Content-Type: application/json"   -d '{"message":"What is my name?", "sessionId":"user1"}'

Configurable System Prompts

The system prompt is where you define your chatbot's personality, role, and constraints. This is the most powerful lever you have for making the chatbot useful for a specific purpose.

Example System Prompts
// Customer service bot
const SYSTEM = `You are a customer service agent for
Acme Corp. Help customers with: orders, returns, and
product questions. If you don't know the answer, say
"Let me connect you with our team" and ask for their
email. Never make up information.`;

// Technical support bot
const SYSTEM = `You are a technical support specialist.
Walk users through troubleshooting step by step. Ask
one clarifying question at a time. Format any code or
commands in code blocks.`;

// Sales assistant
const SYSTEM = `You are a friendly sales assistant for
[Company]. Answer product questions helpfully. When
someone is interested, ask for their contact info to
have a specialist follow up. Never pressure customers.`;
Day 2 Exercise
Build a Multi-Turn Chatbot with a Custom Persona
  1. Update your server.js with the history-tracking code.
  2. Test a multi-turn conversation — confirm it remembers context.
  3. Write a custom system prompt for a chatbot that serves a real use case you care about.
  4. Test the persona: does it stay in character? Does it refuse requests outside its scope?

Day 2 Summary

  • Conversation history is stored server-side and sent with every API request.
  • Session IDs separate conversations from different users.
  • System prompts define the chatbot's role, personality, and constraints — the most important tuning lever.
Finished this lesson?