AI Chat: Setup

Install the tools, set your API keys and understand the shared code used by both modules.

Published: 11/22/2026

Both modules use the same small TypeScript project. Each lesson shows the code it needs, so you can type it in yourself.

Requirements

Create the project

mkdir ai-chat && cd ai-chat
pnpm init
pnpm add @anthropic-ai/sdk openai dotenv
pnpm add -D typescript tsx @types/node

Set "type": "module" in package.json so the files can use top-level await.

Create a .env file in the project root. Never commit it.

ANTHROPIC_API_KEY=your-anthropic-key
OPENAI_API_KEY=your-openai-key
WEATHER_API_KEY=your-weatherapi-key

Folder layout

src/
  common/
    prompt-ref.ts   shared prompts
    utils.ts        weather, date maths and the holiday menu
  anthropic/        Module 1
  openai/           Module 2

Run a script with tsx, for example:

pnpm tsx src/anthropic/basic-chat.ts

Shared code: prompts

Create src/common/prompt-ref.ts. It holds the system prompt and some ready-made holiday requests.

export const christmasMarketPrompt =
  "Suggest a travel destination within Europe where there is a Christmas market that is famous but is not in a big city. I would like to go somewhere that is less than 2 hours from a major airport and has good public transport links. Also tell me what the weather is like there now.";
export const winterSunPrompt =
  "Suggest a travel destination within Europe where I can go for a winter sun holiday. I would like to go somewhere that is less than 2 hours from a major airport and has good public transport links. Also tell me what the weather is like there now.";
export const skiingHolidayPrompt =
  "Suggest a ski resort within Europe for a skiing holiday. I would like to go somewhere with reliable snow and a good range of runs for intermediate skiers. I would like to go somewhere that is less than 2 hours from a major airport and has good public transport links. Also tell me what the weather is like there now.";
export const cultureTripPrompt =
  "Suggest a city within Europe for a culture trip with a good mix of museums, historic architecture and local food. I would like to go somewhere that is less than 2 hours from a major airport and has good public transport links. Also tell me what the weather is like there now.";
export const cityBreakPrompt =
  "Suggest a city within Europe for a short city break of 2 to 3 days. I would like somewhere compact and walkable with plenty to see and do. I would like to go somewhere that is less than 2 hours from a major airport and has good public transport links. Also tell me what the weather is like there now.";
export const basicTravelSystemPrompt =
  "You are a helpful travel assistant. Provide detailed travel suggestions based on user preferences and give a guide to the destination and include distance from the airport.";

Shared code: tools helpers

Create src/common/utils.ts. The tool lessons (Module 1 lesson 3 and Module 2 lessons 3 and 4) import from it.

Imports and weather. getWeather calls the weatherapi.com realtime API. It returns the current observation, not a forecast. The response type is trimmed to a few fields; the API returns more.

import type { Interface } from "node:readline/promises";
import {
  christmasMarketPrompt,
  cityBreakPrompt,
  cultureTripPrompt,
  skiingHolidayPrompt,
  winterSunPrompt,
} from "./prompt-ref.js";

export type WeatherApiCurrentResponse = {
  location: {
    name: string;
    region: string;
    country: string;
    localtime: string;
  };
  current: {
    last_updated: string;
    temp_c: number;
    condition: { text: string };
    wind_kph: number;
    humidity: number;
    feelslike_c: number;
  };
};

export const getWeather = async (
  location: string,
  country: string,
): Promise<WeatherApiCurrentResponse> => {
  const query = `${location}, ${country}`;
  const response = await fetch(
    `https://api.weatherapi.com/v1/current.json?q=${encodeURIComponent(query)}&aqi=no&key=${process.env.WEATHER_API_KEY}`,
  );
  if (!response.ok) {
    throw new Error(
      `Weather API error: ${response.status} ${await response.text()}`,
    );
  }
  return (await response.json()) as WeatherApiCurrentResponse;
};

Date maths. Models are poor at calendar arithmetic, so a tool does it.

export type TimePeriod = "days" | "weeks" | "months" | "years";

export const addTimePeriod = (
  date: Date,
  amount: number,
  unit: TimePeriod,
): Date => {
  const result = new Date(date.getTime()); // don't modify the original
  switch (unit) {
    case "days":
      result.setDate(result.getDate() + amount);
      break;
    case "weeks":
      result.setDate(result.getDate() + amount * 7);
      break;
    case "months":
      result.setMonth(result.getMonth() + amount);
      break;
    case "years":
      result.setFullYear(result.getFullYear() + amount);
      break;
  }
  return result;
};

Holiday menu. askHolidayPrompt prints a numbered menu, reads a choice with readline and returns the matching prompt, or your own request.

const HOLIDAY_OPTIONS = [
  { label: "Christmas market", prompt: christmasMarketPrompt },
  { label: "Winter sun", prompt: winterSunPrompt },
  { label: "Skiing holiday", prompt: skiingHolidayPrompt },
  { label: "Culture trip", prompt: cultureTripPrompt },
  { label: "City break", prompt: cityBreakPrompt },
  { label: "Other (you specify)", prompt: null },
];

export const askHolidayPrompt = async (rl: Interface): Promise<string> => {
  console.log("What type of holiday are you looking for?");
  HOLIDAY_OPTIONS.forEach((o, i) => console.log(`  ${i + 1}. ${o.label}`));

  while (true) {
    const answer = (await rl.question("Choose a number: ")).trim();
    const option = HOLIDAY_OPTIONS[Number(answer) - 1];
    if (!option) {
      console.log(`Please enter a number from 1 to ${HOLIDAY_OPTIONS.length}.`);
      continue;
    }
    if (option.prompt) return option.prompt;

    const custom = (await rl.question("Describe the holiday you want: ")).trim();
    if (custom) return custom;
  }
};

👉 Start with Module 1 (Anthropic)