diff --git a/apps/docs/content/components/(chatbot)/context.mdx b/apps/docs/content/components/(chatbot)/context.mdx index d692c4af..783b196d 100644 --- a/apps/docs/content/components/(chatbot)/context.mdx +++ b/apps/docs/content/components/(chatbot)/context.mdx @@ -12,6 +12,92 @@ The `Context` component provides a comprehensive view of AI model usage through +## Usage with AI SDK + +Use `Context` with the `usage` object returned by AI SDK generation calls. The +API route can return both the model answer and token usage: + +```ts title="app/api/chat/route.ts" +import { streamText } from "ai"; + +export async function POST(req: Request) { + const { prompt } = await req.json(); + + const result = await streamText({ + model: "openai/gpt-4.1-mini", + prompt, + }); + + return Response.json({ + text: await result.text, + usage: await result.usage, + }); +} +``` + +Then render the returned usage data with the compound Context components: + +```tsx title="app/page.tsx" +"use client"; + +import { useState } from "react"; +import { + Context, + ContextCacheUsage, + ContextContent, + ContextContentBody, + ContextContentFooter, + ContextContentHeader, + ContextInputUsage, + ContextOutputUsage, + ContextReasoningUsage, + ContextTrigger, +} from "@/components/ai-elements/context"; +import type { LanguageModelUsage } from "ai"; + +export default function Page() { + const [usage, setUsage] = useState(); + + async function sendMessage() { + const response = await fetch("/api/chat", { + body: JSON.stringify({ prompt: "Summarize this repository" }), + method: "POST", + }); + const data = await response.json(); + setUsage(data.usage); + } + + return ( +
+ + + {usage ? ( + + + + + + + + + + + + + + ) : null} +
+ ); +} +``` + ## Features - **Compound Component Architecture**: Flexible composition of context display elements diff --git a/skills/ai-elements/references/context.md b/skills/ai-elements/references/context.md index 772bb23d..7e24c0f7 100644 --- a/skills/ai-elements/references/context.md +++ b/skills/ai-elements/references/context.md @@ -12,6 +12,92 @@ See `scripts/context.tsx` for this example. npx ai-elements@latest add context ``` +## Usage with AI SDK + +Use `Context` with the `usage` object returned by AI SDK generation calls. The +API route can return both the model answer and token usage: + +```ts title="app/api/chat/route.ts" +import { streamText } from "ai"; + +export async function POST(req: Request) { + const { prompt } = await req.json(); + + const result = await streamText({ + model: "openai/gpt-4.1-mini", + prompt, + }); + + return Response.json({ + text: await result.text, + usage: await result.usage, + }); +} +``` + +Then render the returned usage data with the compound Context components: + +```tsx title="app/page.tsx" +"use client"; + +import { useState } from "react"; +import { + Context, + ContextCacheUsage, + ContextContent, + ContextContentBody, + ContextContentFooter, + ContextContentHeader, + ContextInputUsage, + ContextOutputUsage, + ContextReasoningUsage, + ContextTrigger, +} from "@/components/ai-elements/context"; +import type { LanguageModelUsage } from "ai"; + +export default function Page() { + const [usage, setUsage] = useState(); + + async function sendMessage() { + const response = await fetch("/api/chat", { + body: JSON.stringify({ prompt: "Summarize this repository" }), + method: "POST", + }); + const data = await response.json(); + setUsage(data.usage); + } + + return ( +
+ + + {usage ? ( + + + + + + + + + + + + + + ) : null} +
+ ); +} +``` + ## Features - **Compound Component Architecture**: Flexible composition of context display elements