A real chatbot remembers what was said earlier in the conversation. Day 2 adds conversation history and a configurable system prompt to your backend.
A real chatbot remembers what was said earlier in the conversation. Day 2 adds conversation history and a configurable system prompt to your backend.
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.
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'));
# 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"}'
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.
// 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.`;
Before moving on, confirm understanding of these key concepts: