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.
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:
"moduleResolution": "bundler"
- Modern module resolution"strict": true
- Enables strict type checking"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 dev
during 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 deploy
to 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
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
- Requires
blink login
orBLINK_TOKEN
environment variable - Great for getting started quickly
- Blink handles billing and rate limiting
- More control and transparency
- Direct billing from provider
- 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:Development Workflow
Local Development
- Automatic code reload on file changes
- Environment variable hot-reload from
.env.local
- Reverse tunnel for webhook testing
- Terminal-based chat interface
Deployment
- Bundles your agent code
- Uploads environment variables from
.env.production
- Starts your agent in the cloud
- Provides a public URL
Best Practices
State Management:- Never use module-level variables (Maps, Sets, etc.)
- Always use
agent.store
for 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.local
and.env.production
- Never commit environment files to Git
- Use tool approvals for destructive operations
- Validate all external inputs
- Test locally with
blink dev
before deploying - Verify webhooks with the reverse tunnel
- Check environment variables are set correctly
- Test tool integrations thoroughly
Next Steps
Now that you understand the structure of a Blink agent, you can:- Explore Blink SDKs to add pre-built capabilities
- Check out Agent Use Cases for practical examples
- Read the Building with Blink guide for development tips
- Learn about deployment options