Blank white background with no objects or features visible.

Estamos disponibilizando acesso gratuito ao Gartner Hype Cycle for AI Governance 2026 completo. Obtenha sua cópia →

DSPy Through a Gateway: Cost, Rate Limits and Fallback

By Ashish Dubey

Published: October 14, 2026

⚡ TL;DR
  • DSPy treats prompts as learnable parameters, not hand-written text. You define signatures and modules, then let optimizers tune them against a metric — which means the framework issues a lot of LM calls on your behalf.
  • DSPy calls any model through LiteLLM, so pointing it at a gateway is a one-line change: use the openai/ prefix and set api_base to your gateway URL.
  • Optimization is where a gateway earns its place. A single prediction is one call; compiling a program with BootstrapFewShot or MIPROv2 can be hundreds or thousands — unpredictable spend that token-by-token tracking misses.
  • Through the TrueFoundry AI Gateway you get cost tracking, rate limiting, caching, and fallback across every DSPy call, plus one-string model switching to re-optimize on a cheaper or stronger model.
  • Setup is two lines of config and your DSPy code is unchanged — the gateway sits underneath the LM interface.

What DSPy is

DSPy is a framework from Stanford for building LM systems by programming them rather than prompting them. Instead of hand-tuning prompt strings, you declare the shape of a task — a signature like question -> answer — and compose those into modules (ChainOfThought, ReAct, custom dspy.Module classes). Then DSPy’s optimizers tune the actual prompts and few-shot demonstrations against a metric you define, treating the prompt as a learnable parameter.

This is an effective design, and one which has a cost profile that teams often underestimate. Since optimization entails calling your program to execute the process iteratively on a dataset (creation, evaluation, selection), DSPy is capable of converting small chunks of code into thousands of LM calls. This framework does tasks that you would be performing on your own manually, however, through burning tokens.

This is precisely what an ideal workload for a gateway would be like.

How DSPy calls a model

DSPy routes every LM request through LiteLLM, which means it speaks the OpenAI-compatible protocol. To send that traffic through the TrueFoundry AI Gateway, you name the model with the openai/ prefix and point api_base at your gateway URL.

import dspy

lm = dspy.LM(
    model="openai/anthropic-account/claude-4",  # openai/ prefix + TrueFoundry model name
    api_key="your-truefoundry-api-key",
    api_base="{GATEWAY_BASE_URL}",
)
dspy.configure(lm=lm)

That is the entire integration. Every signature, module, and optimizer you write from here calls the gateway underneath — no other code changes. You can also set it once via environment variables:

export OPENAI_API_KEY="your-truefoundry-api-key"
export OPENAI_BASE_URL="{GATEWAY_BASE_URL}"

DSPy through the TrueFoundry AI Gateway
DSPy through the TrueFoundry AI Gateway

Your DSPy code talks to dspy.LM; dspy.LM talks to the gateway; the gateway talks to whichever provider you named. Swapping openai/anthropic-account/claude-4 for an OpenAI or Gemini model is a string change, because the gateway normalizes them all.

Why optimization makes a gateway worth it

Here is the number that matters. A single ChainOfThought prediction is one or two LM calls. An optimizer is a different animal.

LM calls multiply during DSPy optimization
LM calls multiply during DSPy optimization;

When you call optimizer.compile(), DSPy runs your program over the training set many times — bootstrapping demonstrations, evaluating candidates, searching for the best configuration. A BootstrapFewShot run can be hundreds of calls; MIPROv2 can be thousands. Run that a few times while you iterate on your metric and you have generated serious, bursty, hard-to-predict spend — the kind that blows through a provider rate limit at the worst moment and shows up on the invoice with no breakdown of which experiment caused it.

Calling the model directly, you have no guardrails on any of this. Through the gateway, you do:

What the AI Gateway adds to a DSPy pipeline
What the AI Gateway adds to a DSPy pipeline
  • Cost tracking per optimizer run, model, and team — so you know a MIPROv2 sweep cost $14 before you run forty of them.
  • Rate limiting that stops a compile() from exhausting your provider quota and taking down everything else sharing the key.
  • Caching of repeated calls during optimization, which happen more than you’d think when the same demonstrations are re-scored.
  • Fallback so a provider hiccup mid-compile fails over instead of crashing a two-hour optimization run.
  • One-string model switching, so re-optimizing on a cheaper model to compare quality-per-dollar is a config change.

Where teams get this wrong

They optimize against a raw provider key. The first big compile() either trips a rate limit or produces a surprise bill with no attribution. Route it through a gateway before you scale optimization.

They forget caching during iteration. Re-running optimization while tuning a metric repeats many identical calls. Without a cache you pay for every one.

They hardcode the provider. DSPy’s whole value is comparing configurations — but if the model is baked into every module, comparing providers means code edits. The openai/ prefix plus a gateway makes it a string.

They don’t separate dev from prod spend. Optimization runs are experiments; inference is production. Without per-run cost attribution they blur into one line nobody can explain.

Want to see what a DSPy optimization run actually costs?

How to use DSPy with TrueFoundry

Two steps. Install DSPy (pip install -U dspy), then grab your gateway base URL and model name from the unified code snippet in the TrueFoundry playground.

TrueFoundry playground unified code snippet with base URL and model name
Copy the base URL and model name straight from the playground. Source: TrueFoundry docs.

Configure the LM as shown above, then write ordinary DSPy. A signature and module:

class GenerateAnswer(dspy.Signature):
    """Answer questions with short factoid answers."""
    question = dspy.InputField()
    answer = dspy.OutputField(desc="often between 1 and 5 words")

generate_answer = dspy.ChainOfThought(GenerateAnswer)
pred = generate_answer(question="What is the capital of Brazil?")
print(pred.answer)
And an optimizer run — the part that benefits most from the gateway:
from dspy.evaluate import Evaluate

def validate_answer(example, pred, trace=None):
    return example.answer.lower() in pred.answer.lower()

optimizer = dspy.BootstrapFewShot(metric=validate_answer)
optimized = optimizer.compile(qa_module, trainset=trainset)

Every call that compile() makes is now metered, rate-limited, cached, and fallback-protected by the gateway — with zero changes to the DSPy code itself.

A worked example: budgeting an optimization sweep

A team is tuning a RAG module. Each MIPROv2 run makes roughly 1,500 LM calls, averaging 3,000 input and 400 output tokens per call. They plan 20 runs while iterating on the metric.

On a mid-tier model (say Claude Sonnet 5.5 at $2 / $10 per million), one run is about (1,500 × 3,000 × $2 + 1,500 × 400 × $10) / 1e6 ≈ $15 in input-plus-output — call it $15 per run, or $300 across 20 runs. Route the same optimization at a cheaper model for the bootstrapping phase and the number drops sharply — but you only know that, and can prove the quality trade-off, because the gateway tracked cost per run and per model.

Without that attribution, the $300 is a single unexplained line. With it, optimization becomes a budgeted, measurable part of development — and the decision to re-optimize on a cheaper model is backed by numbers, not vibes.

Gotchas worth knowing

  • Use the openai/ prefix. DSPy goes through LiteLLM; the prefix tells it to use the OpenAI-compatible route to your gateway. Omit it and routing fails.
  • Watch trailing slashes on api_base. A trailing slash can produce a double slash in the request path. Test with a simple call first.
  • api_base vs base_url. Most versions use api_base; confirm which your installed DSPy/LiteLLM accepts.
  • LiteLLM logs can get noisy when pointed at a custom endpoint. If your terminal fills with provider messages, adjust LiteLLM’s logging.
  • Cache during iteration, not just production. The biggest savings in DSPy often come from caching repeated optimization calls.

Related reading

Conclusion

DSPy changes how you build with LLMs — prompts become parameters, and a compiler does the tuning. The hidden cost of that elegance is traffic: optimizers make far more LM calls than the code suggests, and that spend is bursty, unpredictable, and easy to lose track of.

Pointing DSPy at the TrueFoundry AI Gateway is a two-line change that turns all of it into a governed, measured path — cost per optimization run, rate limits that protect your quota, caching that cuts repeated calls, and fallback that keeps a long compile alive. You keep DSPy’s programming model and gain the operational control production needs.

Put your DSPy optimization runs under one roof

Try now.

One gateway for all your models, MCP servers, and agents.
No credit card needed.

Start free
Table of Contents

One Gateway for Every LLM, Agent and MCP Server

Book a 30-min with our AI expert

Book a Demo

The fastest way to build, govern and scale your AI

Book Demo
Summarize with
ChatGPT logo by OpenAI
Perplexity AI logo
Blurry red snowflake on white background, symmetrical frosty design with soft edges and abstract shape.

Discover More

No items found.
October 14, 2026
|
5 min read

The Decisions API Is Now Live on TrueFoundry AI Gateway

No items found.
October 14, 2026
|
5 min read

Deepgram on the AI Gateway: STT, TTS, Cost and Governance

No items found.
October 14, 2026
|
5 min read

DSPy Through a Gateway: Cost, Rate Limits and Fallback

No items found.
October 14, 2026
|
5 min read

Roo Code: Governing a Coding Agent Through a Gateway

No items found.
No items found.

Recent Blogs

Black left pointing arrow symbol on white background, directional indicator.
Black left pointing arrow symbol on white background, directional indicator.

Frequently asked questions

What is DSPy?

DSPy is a framework for building LM systems by programming them instead of hand-writing prompts. You define signatures and modules, then use optimizers to tune the prompts and few-shot examples against a metric.

How do I point DSPy at a gateway?

Use the openai/ prefix in the model name and set api_base to your gateway URL in dspy.LM(...), then dspy.configure(lm=lm). DSPy routes through LiteLLM, so any OpenAI-compatible gateway works.

Why run DSPy through the TrueFoundry AI Gateway?

Because optimization generates large, bursty LM traffic. The gateway adds cost tracking, rate limiting, caching, and fallback across every call, and lets you switch models with a one-string change.

Does using a gateway change my DSPy code?

No. Only the LM configuration changes. Your signatures, modules, and optimizers are untouched.

How much does a DSPy optimization run cost?

It depends on the optimizer, trainset size, and model. A MIPROv2 run can be thousands of LM calls. The gateway meters each run so you can see and budget the exact cost per sweep, per model, and per team.

Can I run this in my own cloud?

Yes. TrueFoundry’s data plane runs in your VPC or on-prem, so DSPy’s prompts and optimization data stay in your environment.

Take a quick product tour
Start Product Tour
Product Tour