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

# DSPy

> Use DSPy to program language models with Together AI.

DSPy is a framework for programming language models rather than relying on static prompts. It enables you to build modular AI systems with code instead of hand-crafted prompting, and it offers methods to automatically optimize these systems.

Features

* Programmatic approach to LLM interactions through Python.
* Modular components for building complex AI pipelines.
* Self-improvement algorithms that optimize prompts and weights.
* Support for various applications from simple classifiers to RAG systems and agent loops.

## Installing libraries

```shell Shell theme={null}
uv add dspy
```

Set your Together API key:

```shell Shell theme={null}
export TOGETHER_API_KEY=***
```

## Example

Set up and connect DSPy to LLMs on Together AI.

```python Python theme={null}
import dspy

# Configure dspy with an LLM from Together AI
lm = dspy.LM(
    "together_ai/Qwen/Qwen3.5-9B",
    api_key=os.environ.get("TOGETHER_API_KEY"),
    api_base="https://api.together.ai/v1",
)

# Now you can call the LLM directly as follows
lm("Say this is a test!", temperature=0.7)  # => ['This is a test!']
lm(
    messages=[{"role": "user", "content": "Say this is a test!"}]
)  # => ['This is a test!']
```

Now you can set up a DSPy module, like `dspy.ReAct` with a task-specific signature. For example, `question -> answer: float` tells the module to take a question and to produce a floating point number answer below.

```python Python theme={null}
# Configure dspy to use the LLM
dspy.configure(lm=lm)


# Gives the agent access to a python interpreter
def evaluate_math(expression: str):
    return dspy.PythonInterpreter({}).execute(expression)


# Gives the agent access to a wikipedia search tool
def search_wikipedia(query: str):

    results = dspy.ColBERTv2(url="http://20.102.90.50:2017/wiki17_abstracts")(
        query, k=3
    )
    return [x["text"] for x in results]


# setup ReAct module with question and math answer signature
react = dspy.ReAct(
    "question -> answer: float",
    tools=[evaluate_math, search_wikipedia],
)

pred = react(
    question="What is 9362158 divided by the year of birth of David Gregory of Kinnairdy castle?"
)

print(pred.answer)
```

## Next steps

> [!NOTE]
>
> ### DSPy - Together AI notebook
>
> Learn more about building agents using DSPy with Together AI in this [notebook](https://github.com/togethercomputer/together-cookbook/blob/main/agents/DSPy/DSPy_Agents.ipynb).


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