MastraModelGateway
Abstract base class for implementing custom model gateways. Gateways handle provider-specific logic for accessing language models, including provider configuration, authentication, URL construction, and model instantiation.
Class overviewDirect link to Class overview
import { MastraModelGateway, type ProviderConfig } from '@mastra/core/llm'
import { createOpenAICompatible } from '@ai-sdk/openai-compatible-v5'
import type { LanguageModelV2 } from '@ai-sdk/provider-v5'
class MyCustomGateway extends MastraModelGateway {
readonly id = 'custom'
readonly name = 'My Custom Gateway'
async fetchProviders(): Promise<Record<string, ProviderConfig>> {
return {
'my-provider': {
name: 'My Provider',
models: ['model-1', 'model-2'],
apiKeyEnvVar: 'MY_API_KEY',
gateway: this.id,
},
}
}
buildUrl(modelId: string, envVars?: Record<string, string>): string {
return 'https://api.my-provider.com/v1'
}
async getApiKey(modelId: string): Promise<string> {
const apiKey = process.env.MY_API_KEY
if (!apiKey) throw new Error('MY_API_KEY not set')
return apiKey
}
async resolveLanguageModel({
modelId,
providerId,
apiKey,
}: {
modelId: string
providerId: string
apiKey: string
}): Promise<LanguageModelV2> {
const baseURL = this.buildUrl(`${providerId}/${modelId}`)
return createOpenAICompatible({
name: providerId,
apiKey,
baseURL,
}).chatModel(modelId)
}
}
Required propertiesDirect link to Required properties
id:
name:
Required methodsDirect link to Required methods
fetchProviders()Direct link to fetchproviders
Fetches provider configurations from the gateway.
Returns: Promise<Record<string, ProviderConfig>>
ProviderConfig Structure:
name:
models:
apiKeyEnvVar:
gateway:
url?:
apiKeyHeader?:
docUrl?:
buildUrl()Direct link to buildurl
Builds the API URL for a specific model/provider combination.
Parameters:
modelId:
envVars?:
Returns: string | undefined | Promise<string | undefined>
getApiKey()Direct link to getapikey
Retrieves the API key for authentication.
Parameters:
modelId:
Returns: Promise<string>
resolveLanguageModel()Direct link to resolvelanguagemodel
Creates a language model instance.
Parameters:
modelId:
providerId:
apiKey:
Returns: Promise<LanguageModelV2> | LanguageModelV2
Instance methodsDirect link to Instance methods
getId()Direct link to getid
Returns the gateway's unique identifier.
Returns: string - The gateway's id property
Model ID formatDirect link to Model ID format
For true gateways, the gateway ID is used as a prefix and models are accessed using this format:
[gateway-id]/[provider]/[model]
Examples:
- Gateway with
id = 'custom':'custom/my-provider/model-1'
Built-in implementationsDirect link to Built-in implementations
- NetlifyGateway - Netlify AI Gateway integration
- ModelsDevGateway - Registry of OpenAI-compatible providers
RelatedDirect link to Related
- Custom Gateways Guide - Complete guide to creating custom gateways
- Mastra.addGateway() - Add a gateway to Mastra
- Mastra.getGateway() - Get gateway by registration key
- Mastra.getGatewayById() - Get gateway by ID
- Mastra.listGateways() - List all gateways