create-agent
Bootstrap a modular AI agent with OpenRouter SDK, extensible hooks, and optional Ink TUI
By openrouterarchived · 2,704 installs
npx skills add openrouterarchived/agent-skills --skill create-agent
Source repository · Upstream listing
Build a Modular AI Agent with OpenRouter
This skill helps you create a modular AI agent with:
Standalone Agent Core Runs independently, extensible via hooks
OpenRouter SDK Unified access to 300+ language models
Optional Ink TUI Beautiful terminal UI (separate from agent logic)
Architecture
Prerequisites
Get an OpenRouter API key at: https://openrouter.ai/settings/keys
⚠️ Security: Never commit API keys. Use environment variables.
Project Setup
Step 1: Initialize Project
Step 2: Install Dependencies
Step 3: Create tsconfig.json
Step 4: Add Scripts to package.json
File Structure
Step 1: Agent Core with Hooks
Create src/agent.ts the standalone agent that can run anywhere:
Step 2: Define Tools
Create src/tools.ts :
Step 3: Headless Usage (No UI)
Create src/headless.ts use the agent programmatically:
Run headless: OPENROUTER API KEY=sk or ... npm run start:headless
Step 4: Ink TUI (Optional Interface)
Create src/cli.tsx a beautiful terminal UI that uses the agent with items based streaming:
Run TUI: OPENROUTER API KEY=sk or ... npm start
Understanding Items Based Streaming
The OpenRouter SDK uses an items based streaming model a key paradigm where items are emitted multiple times with the same ID but progressively updated content. Instead of accumulating chunks, you replace items by their ID .
How It Works
Each iteration of getItemsStream() yields a complete item with updated content:
For function calls, arguments stream progressively:
Why Items Are Better
Traditional (accumulation required):
Items (complete replacement):
Benefits:
No manual chunk management each item is complete
Handles concurrent outputs function calls and messages can stream in parallel
Full TypeScript inference for all item types
Natural Map based state works perfectly with React/UI frameworks
Extending the Agent
Add Custom Hooks
Use with HTTP Server
Use with Discord
Agent API Reference
Constructor Options
Option Type Default Description
apiKey string required OpenRouter API key
model string 'openrouter/auto' Model to use
instructions string 'You are a helpful assistant.' System prompt
tools Tool[] [] Available tools
maxSteps number 5 Max agentic loop iterations
Methods
Method Returns Description
send(content) Promise<string Send message with streaming
sendSync(content) Promise<string Send message without streaming
getMessages() Message[] Get conversation history
clearHistory() void Clear conversation
setInstructions(text) void Update system prompt
addTool(tool) void Add tool at runtime
Events
Event Payload Description
message:user Message User message added
message:assistant Message Assistant response complete
item:update StreamableOutputItem Item emitted (replace by ID, don't accumulate)
stream:start Streaming started
stream:delta (delta, accumulated) New text chunk
stream:end fullText Streaming complete
tool:call (name, args) Tool being called
tool:result (name, result) Tool returned result
reasoning:update text Extended thinking content
thinking:start Agent processing
thinking:end Agent done processing
error Error Error occurred
Item Types (from getItemsStream)
The SDK uses an items based streaming model where items are emitted multiple times with the same ID but progressively updated content. Replace items by their ID rather than accumulating chunks.
Type Purpose
message Assistant text responses
function call Tool invocations with streaming arguments
function call output Results from executed tools
reasoning Extended thinking content
web search call Web search operations
file search call File search operations
image generation call Image generation operations
Discovering Models
Do not hardcode model IDs they change frequently. Use the models API:
Fetch Available Models
Dynamic Model Selection in Agent
Using openrouter/auto
For simplicity, use openrouter/auto which automatically selects the best
available model for your request:
Models API Reference
Endpoint : GET https://openrouter.ai/api/v1/models
Response : { data: OpenRouterModel[] }
Browse models : https://openrouter.ai/models
Resources
OpenRouter Docs: https://openrouter.ai/docs
Models API: https://openrouter.ai/api/v1/models
Ink Docs: https://github.com/vadimdemedes/ink
Get API Key: https://openrouter.ai/settings/keys