Skip to main content

AI Gateway

Route your LLM calls through the AI Gateway with a single base URL change. Zero vendor lock-in: always run on the best model at the lowest cost for your use case.

Observability

Instrument your code with OpenTelemetry to capture traces, logs, and metrics for every LLM call, agent step, and tool use.

AI Gateway

Overview

Mastra is a TypeScript framework for building AI-powered applications with pipelines, agents, and workflows. By connecting Mastra to Toqan Router’s AI Gateway, you transform experimental AI applications into production-ready systems with enterprise-grade capabilities.

Key Benefits

Toqan Router’s AI Gateway enhances your Mastra applications with:

Complete Observability

Track every agent step, tool use, and interaction with detailed traces and analytics

Built-in Reliability

Automatic fallbacks, retries, and load balancing for production resilience

Cost Optimization

Real-time cost tracking and spend management across all your AI operations

Multi-Provider Access

Access 500+ LLMs and 30+ providers through a single, unified integration

Prerequisites

Before integrating Mastra with Toqan Router, ensure you have:
  • A Toqan Router account and API Key
  • Node.js 22.13 or higher
  • TypeScript support
  • Mastra installed in your project
To setup your API key, see API keys & Endpoints.

Installation

Install Mastra:

Configuration

Configure Mastra to use Toqan Router’s AI Gateway with an OpenAI-compatible model configuration:
TypeScript
url: https://router.toqan.ai/eu/v3/router

Basic Agent Example

Here’s a complete example of creating and running a Mastra agent through Toqan Router:
TypeScript

Model Selection

With Toqan Router, you can use any supported model from 30+ providers:
TypeScript

Agent with tools

Define tools with createTool and attach them to an agent through the tools field:
TypeScript

Observability

Getting Started

Integrate Mastra with Toqan Router’s observability to gain complete insights into pipeline execution, agent performance, workflow orchestration, and system reliability using OpenTelemetry.

Prerequisites

Before you begin, ensure you have:
  • A Toqan Router account and API Key
  • Node.js 22.13 or higher and TypeScript support
  • Mastra installed in your project
  • API keys for your LLM providers and external services

Install Dependencies

Configure Toqan Router

Set up your environment variables to connect to Toqan Router’s OpenTelemetry collector: Unix/Linux/macOS:
Windows (PowerShell):
Using .env file:

Custom Instrumentation

Mastra supports native OpenTelemetry integration for comprehensive observability. Create an instrumentation.mjs file in your Mastra project:
TypeScript
Load it when starting the app:
Enable Telemetry in your Mastra initialization:
TypeScript
Register every agent in the agents field of the Mastra constructor. Unregistered agents do not emit traces, because OtelBridge only sees agents passed in at construction time.
All Mastra pipelines and agent calls will be instrumented and exported to Toqan Router through the OTLP exporter.

View Traces

View your traces in the AI Studio in the Traces tab.
Visit your AI Studio to view real-time analytics and traces.