Skip to content

Streaming

llmist is streaming-first. All responses stream by default.

Collect all text into a string:

const answer = await LLMist.createAgent()
.withModel('haiku')
.withGadgets(FloppyDisk)
.askAndCollect('How many floppies for a 10MB file?');
console.log(answer); // "A 10MB file requires 7 floppy disks."

Handle events as they happen:

await LLMist.createAgent()
.withModel('sonnet')
.withGadgets(ArcadeHighScore)
.askWith('Check high scores for Pac-Man', {
onText: (text) => process.stdout.write(text),
onGadgetCall: (call) => console.log(`Calling: ${call.gadgetName}`),
onGadgetResult: (result) => console.log(`Result: ${result.result}`),
onHumanInputRequired: (data) => console.log(`Question: ${data.question}`),
});

Full control with async iteration:

const agent = LLMist.createAgent()
.withModel('gpt4o')
.withGadgets(DialUpModem)
.ask('Connect me to AOL');
for await (const event of agent.run()) {
switch (event.type) {
case 'text':
process.stdout.write(event.content);
break;
case 'gadget_call':
console.log(`\nCalling: ${event.call.gadgetName}`);
break;
case 'gadget_result':
console.log(`Result: ${event.result.result}`);
break;
case 'human_input_required':
console.log(`Question: ${event.question}`);
break;
}
}
TypePropertiesDescription
textcontent: stringText chunk from LLM
gadget_callcall: { gadgetName, parameters }Gadget about to execute
gadget_resultresult: { gadgetName, result?, error?, parameters }Gadget completed
human_input_requiredquestion, gadgetName, invocationIdUser input needed

Import from llmist:

import { collectText, collectEvents, runWithHandlers } from 'llmist';
const text = await collectText(agent.run());
console.log(text);
const { text, gadgetCalls, gadgetResults } = await collectEvents(agent.run(), {
text: true,
gadgetCalls: true,
gadgetResults: true,
});
console.log('Response:', text.join(''));
console.log('Gadgets called:', gadgetCalls.length);
await runWithHandlers(agent.run(), {
onText: (text) => console.log(text),
onGadgetResult: (result) => console.log(result),
});
interface EventHandlers {
onText?: (content: string) => void | Promise<void>;
onGadgetCall?: (call: {
gadgetName: string;
parameters?: Record<string, unknown>;
}) => void | Promise<void>;
onGadgetResult?: (result: {
gadgetName: string;
result?: string;
error?: string;
parameters: Record<string, unknown>;
}) => void | Promise<void>;
onHumanInputRequired?: (data: {
question: string;
gadgetName: string;
}) => void | Promise<void>;
onOther?: (event: StreamEvent) => void | Promise<void>;
}

For simple streaming without agents:

// Static method
for await (const chunk of LLMist.stream('Tell me a story')) {
process.stdout.write(chunk);
}
// Instance method
const client = new LLMist();
for await (const chunk of client.streamText('Tell me a story')) {
process.stdout.write(chunk);
}