Skip to main content
Retrieval-augmented generation (RAG) grounds a language model’s answer in your own documents. At a high level, you embed a corpus of text into vectors, store those vectors, retrieve the closest matches to a user’s query, and pass the retrieved text into a chat completion as context. The model answers from your data instead of guessing. Together AI exposes the three primitives a RAG pipeline needs (embeddings, rerank, and chat completions) behind a single API and SDK. The walkthrough below builds an end-to-end example you can run as-is, then points to deeper material on each piece, common vector store integrations, and existing RAG cookbooks in the Guides tab.

End-to-end example

The script below builds a tiny RAG pipeline with no external dependencies beyond the Together SDK. It embeds a small corpus, stores the vectors in memory, retrieves the top matches by cosine similarity, and passes them into a chat completion as context.
Python
This is the smallest pipeline that’s still recognizably RAG. Real systems chunk longer documents to fit the embedding model’s context limit (514 tokens for intfloat/multilingual-e5-large-instruct), persist vectors in a database, and add a reranking stage to improve precision before generation.

Add a rerank stage

A reranker is a second-stage model that re-scores the top results from your vector search using the query and document together. Rerank improves precision when the top of your similarity ranking is noisy or when you only have room for a few documents in the prompt. See the Rerank guide for details.
Rerank models like mixedbread-ai/mxbai-rerank-large-v2 are only available for dedicated model inference. Spin one up before running the snippet below, then point RERANK_MODEL at it.
To slot reranking in, retrieve more candidates from the vector store than you plan to use, rerank them, and pass the top reranked documents into the chat completion.
Python
The same pattern (over-retrieve, rerank, generate) is what production RAG systems use, regardless of which vector store sits underneath.

Vector store integrations

The in-memory store above is fine for a few hundred documents. For larger corpora, persist your vectors in a dedicated vector database. Together embeddings work with any store that accepts raw float vectors.

Pinecone

Pinecone is a managed vector database with a serverless tier. Embed with Together, then upsert and query through the Pinecone client.
Python
For Pinecone-specific guidance on indexing, namespaces, and metadata filtering, see the Pinecone documentation. MongoDB Atlas adds vector search on top of a regular Mongo collection. Store the embedding alongside the document and define a vector index on the embedding field.
Python
Once your Atlas vector index is configured, query with $vectorSearch in an aggregation pipeline. The full walkthrough is in the MongoDB + Together AI tutorial.

Pixeltable

Pixeltable is a declarative table for unstructured data. It can call Together embeddings as a column expression, so chunking, embedding, and indexing all live in your table definition.
Python
For more, see the Pixeltable + Together docs.

Other frameworks

Together is also a first-class provider in the major LLM application frameworks:

Beyond the basics

Once your pipeline is working, the next questions are usually about chunking strategy, retrieval quality, and evaluation. Start here: For working notebooks, browse the together-cookbook repo on GitHub.