> ## Documentation Index
> Fetch the complete documentation index at: https://docs.neosantara.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# DSPy

> Program and optimize language model prompts algorithmically with Neosantara and DSPy.

[DSPy](https://dspy.ai/?utm_source=neosantara-docs\&utm_medium=referral) (Declarative Self-improving Python) is a framework for programmatically building and optimizing language model pipelines. Instead of manually tuning brittle prompt strings, you define structured input/output *Signatures* and compose workflows using modules like `Predict`, `ChainOfThought`, or `ReAct`. DSPy automatically compiles and optimizes prompt instructions and few-shot demonstrations against your evaluation metrics.

## Setup

Install the DSPy library:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
pip install -U dspy
```

```bash Bash / zsh icon="terminal" theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
export NEOSANTARA_API_KEY="nsk_your_api_key_here"
```

```env .env icon="file-code" theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
NEOSANTARA_API_KEY=nsk_your_api_key_here
```

```powershell PowerShell icon="terminal" theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
$env:NEOSANTARA_API_KEY="nsk_your_api_key_here"
```

## Client Configuration

Initialize your language model using the `dspy.LM` class with the `neosantara/<model>` prefix.

```python theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
import os
import dspy

lm = dspy.LM(
    "neosantara/deepseek-v4.1-flash",
    api_key=os.environ["NEOSANTARA_API_KEY"]
)

dspy.configure(lm=lm)
```

## Basic Modules: Predict and ChainOfThought

```python Simple QA (Predict) icon="python" theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
import os
import dspy

lm = dspy.LM(
    "neosantara/gemini-3.8-flash",
    api_key=os.environ["NEOSANTARA_API_KEY"]
)
dspy.configure(lm=lm)

class BasicQA(dspy.Signature):
    """Answer technical questions concisely."""
    question = dspy.InputField(desc="User question")
    answer = dspy.OutputField(desc="Concise answer in 1-2 sentences")

qa = dspy.Predict(BasicQA)
response = qa(question="What is the primary difference between input and output tokens in an LLM gateway?")

print(response.answer)
```

```python Reasoning (ChainOfThought) icon="python" theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
import os
import dspy

lm = dspy.LM(
    "neosantara/deepseek-v4.1-flash",
    api_key=os.environ["NEOSANTARA_API_KEY"]
)
dspy.configure(lm=lm)

class MathProblem(dspy.Signature):
    """Solve math questions with step-by-step reasoning."""
    problem = dspy.InputField()
    solution = dspy.OutputField()

cot = dspy.ChainOfThought(MathProblem)
result = cot(problem="A server processes 120 requests per second. Each request consumes 400 input tokens. How many input tokens does the server receive per minute?")

print(result.solution)
```

## ReAct Agent with Tools

Use `dspy.ReAct` to build autonomous agents that invoke Python tools before synthesizing a final answer.

```python theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
import os
import dspy

lm = dspy.LM(
    "openai/deepseek-v4.1-flash",
    api_key=os.environ["NEOSANTARA_API_KEY"],
    api_base="https://api.neosantara.xyz/v1"
)
dspy.configure(lm=lm)

def get_server_load(region: str) -> str:
    """Return simulated CPU load and active instances for a region."""
    return f"Region {region}: 34% average CPU utilization, 12 healthy pods."

react = dspy.ReAct(
    "question: str -> answer: str",
    tools=[get_server_load]
)

pred = react(question="What is the current server health in the jkt-1 region?")
print(pred.answer)
```

## Next Steps

* [Chat Completions Guide](https://docs.neosantara.xyz/en/gateway/chat-completions.md)
* [Model Catalog and Pricing](https://docs.neosantara.xyz/en/gateway/models.md)
* [Any-LLM Integration](https://docs.neosantara.xyz/en/integrations/any-llm.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.