Day 02 Core Concepts

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.

~1 hour Hands-on Precision AI Academy

Today's Objective

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

01

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
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'));
02

Testing Multi-Turn Conversation

Test Multi-Turn
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"}'
03

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
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.`;

Supporting References & Reading

Go deeper with these external resources.

Anthropic Docs
Backend: Conversation History and System Prompts Official Claude API documentation and guides.
YouTube
Backend: Conversation History and System Prompts AI chatbot tutorials
MDN
MDN Web Docs Comprehensive web technology reference

Day 2 Checkpoint

Before moving on, confirm understanding of these key concepts:

Continue To Day 3
Day 3 of the Build an AI Chatbot course