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

# AI SDK

> How AnotherWrapper uses the Vercel AI SDK across chat, structured generation, vision, and more

<Info>
  Think of the AI SDK as the translation layer between your app and different model providers. It lets you talk to OpenAI, Anthropic, Google, xAI, Groq, DeepSeek, and Replicate through one consistent developer experience.
</Info>

## Why This Matters

Without an abstraction layer, every provider has a different API shape, a different streaming format, and different quirks. The Vercel AI SDK means you can:

* Stream chat responses in real time
* Switch models without rebuilding your whole app
* Generate structured JSON safely
* Run tool calls inside chat
* Generate embeddings for document search
* Keep your codebase *way* cleaner than a pile of provider-specific SDK logic

## Where the Repo Uses It

The AI SDK isn't just used in one demo. It powers multiple core parts of the product:

* **Chat** -- `streamText()` + `useChat()` for streaming conversations, tool calling, browsing, and reasoning
* **Marketing Plan & Launch Simulator** -- `generateText()` with `Output.object()` for validated JSON output
* **Vision** -- `generateText()` with a schema so meal photos come back as structured nutrition data
* **Audio** -- Structured generation for summaries and action items after transcription
* **RAG** -- `embed()` and `embedMany()` to convert document text into vectors for semantic retrieval

## The Main Patterns

<Tabs>
  <Tab title="Streaming Chat">
    This is what makes the chat app feel alive instead of waiting for one big response at the end.

    **The flow:**

    1. The user sends a message
    2. The server prepares the system prompt, tools, and optional document context
    3. `streamText()` starts streaming tokens back immediately
    4. `useChat()` updates the UI as the answer arrives
    5. The final assistant message is stored in PostgreSQL

    That's the backbone of the flagship chat app. Users get instant feedback and the conversation persists across sessions.
  </Tab>

  <Tab title="Structured Outputs">
    For things like the nutrition analyzer, marketing planner, or launch simulator, plain text isn't enough. You want fields like `title`, `summary`, `calories`, or `actionItems` in a predictable shape.

    The repo uses **schema-based outputs**: the model returns data matching a Zod schema, and your app renders it cleanly. This is a huge quality-of-life upgrade compared to parsing random AI text after the fact.

    <Tip>
      If you're building a new app that needs structured data, duplicate one of the existing schema-based generation routes and tailor its prompt, schema, and UI. Much faster than starting from scratch.
    </Tip>
  </Tab>

  <Tab title="Provider Switching">
    AnotherWrapper ships with provider adapters for:

    * OpenAI
    * Anthropic
    * Google
    * Groq
    * xAI
    * DeepSeek
    * Replicate

    You can expose several models in one UI and let users switch between them -- without rebuilding each app from scratch. Just add the provider key and register the models.
  </Tab>

  <Tab title="Embeddings for RAG">
    The repo uses the AI SDK for embeddings in the document chat system:

    1. Text from a PDF is split into chunks
    2. Each chunk is converted into a vector via `embed()` / `embedMany()`
    3. Vectors are stored in PostgreSQL with `pgvector`
    4. When the user asks a question, the question is also embedded
    5. The app finds the most relevant chunks and injects them into the prompt

    That's how the chat app answers questions about your uploaded documents instead of only relying on the model's general knowledge.
  </Tab>
</Tabs>

## What You Need to Set Up

At minimum, you need **one provider key**. For most users, the easiest start is:

* [OpenAI](/providers/openai) for chat, structured generation, and embeddings

Then add more providers only if you want more model choice:

<CardGroup cols={3}>
  <Card title="Anthropic" icon="microchip" href="/providers/anthropic" />

  <Card title="Google" icon="microchip" href="/providers/google" />

  <Card title="Groq" icon="microchip" href="/providers/groq" />

  <Card title="xAI" icon="microchip" href="/providers/xai" />

  <Card title="DeepSeek" icon="microchip" href="/providers/deepseek" />

  <Card title="Replicate" icon="microchip" href="/providers/replicate" />
</CardGroup>

## Files to Know

<AccordionGroup>
  <Accordion title="Model catalog and provider setup">
    * `package.json` -- installed AI SDK packages
    * `lib/ai/models.ts` -- the shared model catalog
  </Accordion>

  <Accordion title="Chat and tool calling">
    * `app/(apps)/chat/api/chat/*` -- streaming chat logic
    * `app/(apps)/chat/tools/*` -- tool calling implementations
  </Accordion>

  <Accordion title="Structured generation apps">
    * `app/(apps)/marketing-plan/api/route.ts` -- growth plan generation
    * `app/(apps)/launch-simulator/api/route.ts` -- schema-based launch simulations
    * `app/(apps)/vision/api/route.ts` -- structured vision output
  </Accordion>

  <Accordion title="Embeddings and RAG">
    * `lib/rag/*` -- embeddings and retrieval pipeline
  </Accordion>
</AccordionGroup>

## Good First Customizations

If you're building on top of this starter, here's what people usually do first:

1. Remove providers you don't plan to support
2. Add or remove models from the shared model list
3. Change which models are free vs. credit-gated
4. Tighten prompts for your specific use case
5. Duplicate a schema-based generation app and tailor its prompt, schema, and UI
6. Add new tools to the chat app

## Common Mistakes

<Warning>
  Watch out for these pitfalls:

  * Adding provider keys but forgetting to expose the models in the UI
  * Assuming every model supports browsing, vision, or thinking controls
  * Treating AI SDK abstractions like magic and forgetting provider-level costs
  * Using free-form text when a structured schema would be safer
  * Enabling document chat without also setting up storage and embeddings
</Warning>

<CardGroup cols={3}>
  <Card title="Chat" icon="robot" href="/apps/chat">
    See the flagship app that uses streaming, tool calling, browsing, and RAG.
  </Card>

  <Card title="Vector RAG" icon="database" href="/services/vector-rag">
    Learn how embeddings and document retrieval work in this repo.
  </Card>

  <Card title="OpenAI" icon="microchip" href="/providers/openai">
    OpenAI is the easiest provider to set up first.
  </Card>
</CardGroup>
