Lesson

Create Your First Custom AI Agent in Minutes

Learn to build reusable AI agents with Claude CLI. Start with a French-speaking bot, Bun.spawn, markdown prompts, and TypeScript setup.

Access
Included
Transcript
Needs source

Stop repeating yourself and start automating. This lesson is your first step into building specialized AI agents. You'll learn the foundational skill of creating a simple, reusable "French-speaking" agent by spawning the Claude CLI with a custom system prompt. We'll cover the essential mechanics of using Bun.spawn to create an interactive session, how to externalize prompts in markdown files for easy editing, and the TypeScript setup needed to tie it all together. This is the core workflow we'll build on throughout the course.

How it Works

  1. Spawn a Process: Use Bun.spawn to run the claude CLI, inheriting standard I/O to create an interactive session directly in your terminal.
  2. Externalize Prompts: Store system prompts in simple markdown (.md) files. This separates the agent's "personality" from the code.
  3. Import Prompts as Text: Leverage Bun's ability to import non-JavaScript files. We'll import the .md file with a type: "text" assertion.
  4. Handle TypeScript Types: Create a custom type declaration file (types.d.ts) to inform TypeScript how to handle .md module imports, resolving any "Cannot find module" errors.
  5. Dynamically Build Flags: Construct command-line arguments programmatically from a configuration object. This makes it easy to add or change the flags passed to the claude process.
  6. Launch the Agent: Run the script to launch a custom Claude agent that consistently follows the instructions from your external prompt file.

This workflow provides a powerful and scalable pattern for building a suite of specialized AI agents, making your development process with AI more organized and efficient.

Spoken Prompts

Dictating the content for the markdown prompt file:

Please always speak in French.

Querying the French-speaking agent:

How are you doing today?

Terminal Commands

Initialize a new Bun project:

bun init

Run the agent script:

bun run agents/french.ts

Code Snippets

Creating a custom type declaration in types.d.ts to allow importing .md files as modules in TypeScript.

declare module "*.md" {
  const content: string;
  export default content;
}

Importing the content of a markdown file as a text string using Bun's import attributes.

import prompt from "../prompts/french.md" with {
  type: "text"
};

Dynamically creating an array of command-line arguments from a configuration object to be passed to Bun.spawn.

const flags = {
  "append-system-prompt": prompt
}

const flagsArray = Object.entries(flags).flatMap(([key, value]) => {
  return [`--${key}`, value]
})

Spawning the claude CLI process with the dynamically generated arguments using the spread operator.

Bun.spawn(["claude", ...flagsArray], {
  stdio: ["inherit", "inherit", "inherit"],
});