The ModelRunner class is the core component for executing AI model requests across different providers. It provides a unified interface for running models with support for multiple providers, retry logic, JSON mode, and tool calling.
ModelRunner abstracts the complexity of interacting with different AI providers (Cloudflare, Groq, etc.) by providing a consistent API. It handles:
- Provider-specific execution logic
- Input validation
- Capability checking (JSON mode, tools)
- Retry logic with exponential backoff
- Response building and formatting
constructor(environment: ModelRunnerEnvironment)Creates a new ModelRunner instance with the specified environment configuration.
Parameters:
environment: The environment object containing provider-specific configurations (API keys, endpoints, etc.)
Executes a model request with the specified parameters.
async run<T>({
messages,
model,
jsonMode = false,
options = {}
}: ModelRunnerRunParams): Promise<ModelRunnerResult<T>>Parameters:
messages: Array of message objects withroleandcontentpropertiesmodel: Object specifyingprovider(e.g., 'cloudflare', 'groq') andmodelnamejsonMode(optional): Boolean to enable JSON response mode (default:false)options(optional): Additional provider-specific options includingtools
Returns:
Promise<ModelRunnerResult<T>>: Result object containing:content: The model's response contentisJson: Whether the response is in JSON formattool_calls: Array of tool calls (if any)raw: Raw provider responseusage: Token usage statistics
Example:
const runner = new ModelRunner({
AI: { run: async (model, params) => ({ response: 'Hello!' }) },
GROQ_API_KEY: 'your-api-key',
});
const result = await runner.run({
messages: [{ role: 'user', content: 'Hello, how are you?' }],
model: { provider: 'cloudflare', model: 'llama-2-7b' },
jsonMode: false,
});
console.log(result.content); // 'Hello!'Returns a list of all supported provider names.
static getSupportedProviders(): string[]Example:
const providers = ModelRunner.getSupportedProviders();Returns the capabilities of a specific provider.
static getProviderCapabilities(provider: ModelRunnerProvider): {
jsonMode: boolean;
tools: boolean;
streaming: boolean;
} | undefinedParameters:
provider: The provider name (e.g., 'cloudflare', 'groq')
Returns:
- Object containing capability flags, or
undefinedif provider not found
Example:
const capabilities = ModelRunner.getProviderCapabilities('cloudflare');
// Returns: { jsonMode: true, tools: true, streaming: false }
const groqCapabilities = ModelRunner.getProviderCapabilities('groq');
// Returns: { jsonMode: true, tools: true, streaming: true }Updates the runtime configuration for retry logic, timeouts, caching, and logging.
static updateRuntimeConfig(
config: Partial<RuntimeConfig>
): voidParameters:
config: Partial configuration object with properties:timeout: Request timeout in millisecondsretries: Retry configuration (maxAttempts,baseDelay,maxDelay)caching: Cache configuration (enabled,ttl)logging: Logging configuration (enabled,level)
Example:
// Update timeout
ModelRunner.updateRuntimeConfig({ timeout: 60000 });
// Update retry configuration
ModelRunner.updateRuntimeConfig({
retries: { maxAttempts: 5, baseDelay: 1000, maxDelay: 10000 },
logging: { enabled: true, level: 'debug' },
});import { ModelRunner } from '@em3odme/agentic';
const environment = {
AI: {
run: async (model, params) => ({
response: 'I am doing well, thank you!',
}),
},
};
const runner = new ModelRunner(environment);
const result = await runner.run({
messages: [{ role: 'user', content: 'How are you?' }],
model: { provider: 'cloudflare', model: 'llama-2-7b' },
});
console.log(result.content); // 'I am doing well, thank you!'const result = await runner.run({
messages: [{ role: 'user', content: 'Give me a JSON object with status' }],
model: { provider: 'cloudflare', model: 'test-model' },
jsonMode: true,
});
console.log(result.isJson); // true
console.log(result.content); // Parsed JSON contentconst result = await runner.run({
messages: [{ role: 'user', content: 'What is the weather?' }],
model: { provider: 'cloudflare', model: 'test-model' },
options: { tools: true },
});
console.log(result.tool_calls); // Array of tool callstry {
const result = await runner.run({
messages: [{ role: 'user', content: 'test' }],
model: { provider: 'cloudflare', model: 'test-model' },
});
} catch (error) {
console.error('Model execution failed:', error.message);
}| Provider | JSON Mode | Tools | Streaming |
|---|---|---|---|
| cloudflare | ✓ | ✓ | ✗ |
| groq | ✓ | ✓ | ✓ |
The ModelRunner performs automatic validation:
- Input Validation: Validates provider, model, and options before execution
- Capability Validation: Ensures the provider supports requested features (JSON mode, tools)
- Environment Validation: Verifies required environment variables are present
If validation fails, a ConfigurationError is thrown with descriptive messages.
The ModelRunner includes built-in retry logic with exponential backoff. By default:
- Maximum attempts: 3
- Base delay: 1000ms
- Maximum delay: 10000ms
Configure via updateRuntimeConfig().
interface ModelRunnerRunParams {
messages: Array<{ role: string; content: string }>;
model: { provider: ModelRunnerProvider; model: string };
jsonMode?: boolean;
options?: Record<string, unknown>;
}interface ModelRunnerResult<T> {
content: T;
isJson: boolean;
tool_calls: unknown[];
raw: unknown;
usage?: unknown;
}