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:- TrueFoundry Account: Create a Truefoundry account and follow the instructions in our Gateway Quick Start Guide
- 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 theLM interface. Configure the LM with TrueFoundry’s gateway URL and your API key:

your-truefoundry-api-keywith your actual TrueFoundry API key (if required)your-truefoundry-gateway-urlwith your TrueFoundry Gateway URLopenai/anthropic-account/claude-4with your desired model using theopenai/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
- Cost Tracking: Monitor and track costs across all your DSPy operations with detailed metrics
- Security: Enhanced security with centralized API key management
- Access Controls: Implement fine-grained access controls for different teams
- Rate Limiting: Prevent API quota exhaustion with intelligent rate limiting
- Fallback Support: Automatic failover to alternative providers when needed
- Analytics: Detailed analytics and monitoring for all LLM calls in your DSPy pipelines
- Multi-Provider Support: Seamlessly switch between different model providers (OpenAI, Anthropic, Google, etc.)
- Performance Optimization: Track and optimize the performance of your DSPy modules