Skip to main content
This guide provides instructions for integrating DSPy with the Truefoundry AI Gateway.

What is DSPy?

DSPy is a framework for algorithmically optimizing language model prompts and weights through a programming-first approach. It enables developers to build and optimize LM-based systems by treating prompts as learnable parameters rather than manually crafted text, using a declarative programming model that separates logic from optimization.

Prerequisites

Before integrating DSPy with TrueFoundry, ensure you have:
  1. TrueFoundry Account: Create a Truefoundry account and follow the instructions in our Gateway Quick Start Guide
  2. DSPy Installation: Install DSPy using pip: pip install -U dspy

Setup Process

1. Configure DSPy with TrueFoundry Gateway

DSPy integrates seamlessly with TrueFoundry’s gateway through the LM interface. Configure the LM with TrueFoundry’s gateway URL and your API key:
You will get your base URL and model name directly from the unified code snippet:
TrueFoundry playground showing unified code snippet with base URL and model name
Replace:
  • your-truefoundry-api-key with your actual TrueFoundry API key (if required)
  • {GATEWAY_BASE_URL} with your TrueFoundry Gateway Base URL
  • openai/anthropic-account/claude-4 with your desired model using the openai/ prefix

2. Environment Variables Configuration

For persistent configuration across your DSPy applications, set these environment variables:

Usage Examples

Basic DSPy with TrueFoundry Gateway

Here’s a simple example demonstrating DSPy with TrueFoundry integration:

DSPy Signatures and Modules

Create more sophisticated DSPy programs using signatures and modules:

Advanced RAG System with DSPy

Build a complete RAG (Retrieval-Augmented Generation) system:

DSPy Optimization with TrueFoundry

Optimize your DSPy programs using the built-in optimizers:

Benefits of Using TrueFoundry Gateway with DSPy

  1. Cost Tracking: Monitor and track costs across all your DSPy operations with detailed metrics
  2. Security: Enhanced security with centralized API key management
  3. Access Controls: Implement fine-grained access controls for different teams
  4. Rate Limiting: Prevent API quota exhaustion with intelligent rate limiting
  5. Fallback Support: Automatic failover to alternative providers when needed
  6. Analytics: Detailed analytics and monitoring for all LLM calls in your DSPy pipelines
  7. Multi-Provider Support: Seamlessly switch between different model providers (OpenAI, Anthropic, Google, etc.)
  8. Performance Optimization: Track and optimize the performance of your DSPy modules