For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://modelgates.ai/docs/_mcp/server.
Call Model (Typescript)
Why callModel?
- Items-Based Model: Built on ModelGates's Responses API with structured items (messages, tool calls, reasoning) instead of raw message chunks
- Multiple Consumption Patterns: Get text, stream responses, or access structured data - all from a single call
- Automatic Tool Execution: Define tools with Zod schemas and let the SDK handle execution loops
- Type Safety: Full TypeScript inference for tool inputs, outputs, and events
- Format Compatibility: Convert to/from OpenAI chat and Anthropic Claude message formats
- Streaming First: Built on a reusable stream architecture that supports concurrent consumers
Quick Start
typescript
import { ModelGates } from '@modelgates/agent'; const modelgates = new ModelGates({ apiKey: process.env.MODELGATES_API_KEY,}); const result = modelgates.callModel({ model: 'openai/gpt-5-nano', input: 'What is the capital of France?',}); // Get text (simplest pattern)const text = await result.getText();console.log(text); // "The capital of France is Paris."Consumption Patterns
callModel returns a ModelResult object that provides multiple ways to consume
the response:
Text Methods
typescript
// Get just the text contentconst text = await result.getText(); // Get the full response with usage dataconst response = await result.getResponse();console.log(response.usage); // { inputTokens, outputTokens, cachedTokens }Streaming Methods
typescript
// Stream text deltasfor await (const delta of result.getTextStream()) { process.stdout.write(delta);} // Stream reasoning (for reasoning models)for await (const delta of result.getReasoningStream()) { console.log('Reasoning:', delta);} // Stream complete items by ID (recommended)for await (const item of result.getItemsStream()) { console.log('Item update:', item.type, item.id);} // Stream all events (including tool preliminary results)for await (const event of result.getFullResponsesStream()) { console.log('Event:', event.type);}Tool Methods
typescript
// Get all tool calls from the responseconst toolCalls = await result.getToolCalls(); // Stream tool calls as they completefor await (const toolCall of result.getToolCallsStream()) { console.log(`Tool: $`, toolCall.arguments);} // Stream tool deltas and preliminary resultsfor await (const event of result.getToolStream()) { if (event.type === 'delta') { process.stdout.write(event.content); } else if (event.type === 'preliminary_result') { console.log('Progress:', event.result); }}Input Formats
callModel accepts multiple input formats:
typescript
// Simple stringconst result1 = modelgates.callModel({ model: 'openai/gpt-5-nano', input: 'Hello!',}); // Message array (OpenResponses format)const result2 = modelgates.callModel({ model: 'openai/gpt-5-nano', input: [ { role: 'user', content: 'Hello!' }, ],}); // With system instructionsconst result3 = modelgates.callModel({ model: 'openai/gpt-5-nano', instructions: 'You are a helpful assistant.', input: 'Hello!',});What's Next?
Explore the guides to learn more about specific features:
- Working with Items - Understand the items-based streaming paradigm
- Text Generation - Input formats, model selection, and response handling
- Streaming - All streaming methods and patterns
- Tools - Creating typed tools with Zod schemas and multi-turn orchestration
- nextTurnParams - Tool-driven context injection for skills and plugins
- Message Formats - Converting to/from OpenAI and Claude formats
- Dynamic Parameters - Async functions for adaptive behavior
- Stop Conditions - Intelligent execution control
- API Reference - Complete type definitions and method signatures
Example Tools
Ready-to-use tool implementations:
- Weather Tool - Basic API integration
- Skills Loader - Claude Code skills pattern