Run Agent Streaming

This example demonstrates how to run an agent with streaming response.


Run Agent Streaming Example

Run Agent Streaming Example

import 'dotenv/config'; import {getRunner, Langbase} from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { // Get readable stream const {stream} = await langbase.agent.run({ stream: true, apiKey: process.env.OPENAI_API_KEY!, model: 'openai:gpt-4.1-mini', instructions: 'You are a helpful assistant that help users summarize text.', input: [ { role: 'user', content: 'Who is an AI Engineer?' } ] }); // Convert the stream to a stream runner. const runner = getRunner(stream); // Method 1: Using event listeners runner.on('connect', () => { console.log('Stream started.\n'); }); runner.on('content', content => { process.stdout.write(content); }); runner.on('end', () => { console.log('\nStream ended.'); }); runner.on('error', error => { console.error('Error:', error); }); } main();