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 with atleast one model provider and generate a Personal Access Token by following the instructions in Generating Tokens. For a quick setup guide, see our Gateway Quick Start
- DSPy Installation: Install DSPy using pip:
pip install -U dspy
Setup Process
1. Generate Your TrueFoundry Access Token
Navigate to your TrueFoundry dashboard and generate a Personal Access Token:
2. 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-key
with your actual TrueFoundry API keyyour-truefoundry-gateway-url
with your TrueFoundry Gateway URLopenai/anthropic-account/claude-4
with your desired model using theopenai/
prefix

3. 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