To build an autonomous agent, instantiate RalphLoopAgent with a model, instructions, and tools. Use agent.loop() to run the agent until it satisfies the verifyCompletion condition or hits a stopWhen limit.
Note: zod is used to define tool parameters.
import { RalphLoopAgent, iterationCountIs } from 'ralph-loop-agent';
import { tool } from 'ai';
import { z } from 'zod';
const tools = {
markComplete: tool({
description: 'Mark the task as complete',
parameters: z.object({ summary: z.string() }),
execute: async ({ summary }) => ({ complete: true, summary }),
}),
};
const agent = new RalphLoopAgent({
model: 'anthropic/claude-opus-4.5',
instructions: 'You are a coding assistant. Complete tasks and use markComplete when done.',
tools,
stopWhen: iterationCountIs(20),
verifyCompletion: async ({ result }) => {
for (const step of result.steps) {
for (const toolResult of step.toolResults) {
if (toolResult.toolName === 'markComplete') {
return { complete: true, reason: 'Task marked complete' };
}
}
}
return { complete: false, reason: 'Continue working' };
},
});
const result = await agent.loop({
prompt: 'Create a hello world function in hello.ts',
});