Skip to main content

AI Gateway

Overview

DSPy is a framework for programmatically optimizing LLM prompts and weights through composable modules and signatures. Connecting DSPy to Orq.ai’s AI Gateway provides access to 300+ models for prompt optimization pipelines with a single configuration change.

Key Benefits

Orq.ai’s AI Gateway enhances DSPy applications with:

Complete Observability

Track every signature execution, module call, and optimization step

Built-in Reliability

Automatic fallbacks, retries, and load balancing for production resilience

Cost Optimization

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

Multi-Provider Access

Access 300+ LLMs and 20+ providers through a single, unified integration

Prerequisites

Before integrating DSPy with Orq.ai, ensure the following are in place:
  • An Orq.ai account and API Key
  • Python 3.8 or higher
To set up an API key, see API keys & Endpoints.

Installation

Configuration

Configure DSPy to use Orq.ai’s AI Gateway with dspy.LM:
Python
api_base: https://api.orq.ai/v3/router
DSPy uses LiteLLM internally, which automatically prepends openai/ to the model name when routing to a custom api_base. Since the AI Gateway also requires the openai/ provider prefix, the model name must be double-prefixed so the final identifier reaching the gateway is correct: gpt-4oopenai/openai/gpt-4o.

Basic Example

Python

Chain of Thought

Use ChainOfThought for step-by-step reasoning:
Python

Model Selection

With Orq.ai, any supported model from 20+ providers can be used:
Python