Overview
When you runblink init, Blink scaffolds a complete agent project with all the files you need to start building. This page explains what each file does and how the pieces fit together.
Understanding the structure of a Blink agent helps you navigate the codebase, add new features, and customize your agent’s behavior effectively.
Template Selection
During initialization,blink init prompts you to choose a template:
- Scratch - Basic agent with an example IP tool, ideal for building custom agents from the ground up
- Slack Bot - Pre-configured Slack integration with webhook handling and message processing
Project Files
Runningblink init creates the following files in your directory:
agent.ts
The heart of your agent - This is where your agent’s logic lives. It defines how your agent handles chat messages, processes requests, and integrates with external services.
package.json
Project configuration - Defines your project metadata, dependencies, and npm scripts.
"type": "module"enables ES module syntax"main": "agent.ts"tells Blink where your agent code is- Scripts provide convenient commands for development and deployment
tsconfig.json
TypeScript configuration - Configures the TypeScript compiler with settings optimized for Blink agents.
Key settings:
"module": "Preserve"- Preserve original module syntax"moduleResolution": "bundler"- Modern module resolution"strict": true- Enables strict type checking"noUncheckedIndexedAccess": true- Safer array/object access"noImplicitOverride": true- Explicit override declarations"noEmit": true- No compilation output (Blink handles bundling)"types": ["node"]- Node.js type definitions
.env.local
Development environment variables - Stores API keys and configuration for local development.
- Used by
blink devduring development - Automatically hot-reloaded when changed
- Never committed to version control
.env.production
Production environment variables - Stores secrets for your deployed agent.
- Used by
blink deployto set production secrets - Automatically encrypted and stored in Blink Cloud
- Never committed to version control
.gitignore
Git ignore patterns - Tells Git which files to exclude from version control.
AGENTS.md
Agent development guide - A comprehensive reference document that provides context and instructions for the Edit Mode agent (the agent that helps you build agents).
This file contains:
- Communication guidelines
- Agent development best practices
- Technical knowledge about Blink APIs
- Code quality standards
Understanding agent.ts
The agent.ts file is structured around a few key concepts. Let’s break down the default template:
Complete Default Agent
This example uses the Vercel AI Gateway for model configuration (simple string-based syntax). For Anthropic and OpenAI direct provider configurations, see the Model Configuration section below.Core Components
1. Agent Instance
- HTTP server lifecycle
- Chat state management
- Event routing
- Storage and persistence
2. Chat Event Handler
chat event is triggered whenever:
- A user sends a message
- The model makes tool calls (automatically loops)
- External services trigger a chat via webhooks
messages- Array of all messages in the conversationid- Unique chat identifierabortSignal- For cancellation support
streamText() result, Response, or ReadableStream.
3. Model Configuration
Option 1: Vercel AI GatewayAI_GATEWAY_API_KEY in your .env.local or .env.production file.
- Great for getting started quickly
- Simple string-based model selection
- Works with multiple providers
ANTHROPIC_API_KEY in your .env.local or .env.production file.
OpenAI:
OPENAI_API_KEY in your .env.local or .env.production file.
Direct provider SDKs offer:
- More control and transparency
- Direct billing from provider
- Access to provider-specific features
- Better for production deployments
4. System Prompt
- Personality and tone
- Capabilities and limitations
- Instructions for behavior
- Context about available tools
- Be specific about what the agent should and shouldn’t do
- Describe available tools and when to use them
- Include any domain-specific knowledge
- Keep it focused and concise
5. Message Conversion
convertToModelMessages function handles:
- Format transformation
- Tool call conversion
- Filtering incomplete tool calls
6. Tools
- description - Tells the model what the tool does
- inputSchema - Validates parameters using Zod
- execute - The actual implementation
7. Server Initialization
- Listens for incoming requests
- Routes events to handlers
- Manages WebSocket connections
- Handles graceful shutdown
Event Handlers
Blink agents can handle multiple types of events:Chat Events
HTTP Request Events
- Webhook endpoints (Slack, GitHub, etc.)
- OAuth callbacks
- Custom API endpoints
- Request interception
UI Options Events
Error Events
Chat Management
Blink automatically manages chat state, but you can control it for advanced use cases:Creating Chats
- Unique for each conversation context
- Serializable (strings, numbers, arrays, objects)
- Consistent across requests
Sending Messages
- interrupt - Stop current processing, handle immediately
- enqueue - Queue message, process after current chat finishes
- append - Add to history without triggering processing
Storage API
Persist data across agent restarts:- OAuth tokens
- User preferences
- Cache data
- Chat associations
- Rate limiting counters
Advanced Patterns
Multiple Event Handlers
Tool Approvals
Require manual approval for destructive operations:Tool Context
Add authentication or configuration to SDK tools:Tool Prefixing
Avoid naming collisions when combining multiple SDKs:Best Practices
State Management:- Never use module-level variables (Maps, Sets, etc.)
- Always use
agent.storefor persistent state - Agent processes restart on code changes
- Write clear, specific descriptions
- Use Zod to validate all parameters
- Return structured, parseable data
- Handle errors gracefully
- Be specific about agent behavior
- Describe available tools and when to use them
- Include domain-specific context
- Keep prompts focused and concise
- Store secrets in
.env.localand.env.production - Never commit environment files to Git
- Use tool approvals for destructive operations
- Validate all external inputs
- Test locally with
blink devbefore deploying - Verify webhooks with the reverse tunnel
- Check environment variables are set correctly
- Test tool integrations thoroughly