Open History to see the last commit
1---
2title: A chat box with no tools
3id: ui-recipe-basic-chat
4order: 1
5description: "The smallest React chat UI that works: a layout, a message, a text part, and a composer."
6keywords:
7 - tanstack ai
8 - createChatHook
9 - chat ui
10 - example
11---
12
13This recipe is a chat UI with four components: `layout`, `message`, `text`, and `input`. It has no tools, no interrupts, and no styles.
14
15For a full TanStack Start app with your own API key, read the [Basic Chat](../../tutorials/basic-chat) tutorial.
16
17## Server
18
19Put `OPENAI_API_KEY` in your server environment. Then add a `POST` route at `/api/chat`:
20
21```tsx
22import {
23 chat,
24 chatParamsFromRequest,
25 toServerSentEventsResponse,
26} from '@tanstack/ai'
27import { openaiText } from '@tanstack/ai-openai'
28
29export async function POST(request: Request) {
30 const { messages, threadId, runId } = await chatParamsFromRequest(request)
31
32 const stream = chat({
33 adapter: openaiText('gpt-6.1-sol'),
34 messages,
35 threadId,
36 runId,
37 })
38
39 return toServerSentEventsResponse(stream)
40}
41```
42
43## Client
44
45```tsx
46import { fetchServerSentEvents } from '@tanstack/ai-react'
47import { createChatHook } from '@tanstack/ai-react/ui'
48
49const chatOptions = {
50 connection: fetchServerSentEvents('/api/chat'),
51}
52
53const { useAppChat, useChatContext } = createChatHook({
54 options: chatOptions,
55 components: {
56 input: () => {
57 const chat = useChatContext()
58 return (
59 <form
60 onSubmit={(event) => {
61 event.preventDefault()
62 const field = event.currentTarget.elements.namedItem('message')
63 if (!(field instanceof HTMLInputElement)) return
64 const text = field.value.trim()
65 if (!text) return
66 field.value = ''
67 void chat.sendMessage(text)
68 }}
69 >
70 <input name="message" />
71 <button type="submit">Send</button>
72 </form>
73 )
74 },
75 layout: ({ Messages, Input }) => (
76 <main>
77 <Messages />
78 <Input />
79 </main>
80 ),
81 message: ({ message, Parts }) => (
82 <article data-role={message.role}>
83 <Parts />
84 </article>
85 ),
86 },
87 partsComponents: {
88 text: ({ part }) => <p>{part.content}</p>,
89 fallback: () => null,
90 },
91})
92
93export function ChatScreen() {
94 const chat = useAppChat({ threadId: 'support-chat' })
95 return <chat.AppChat />
96}
97```
98
99Render `<ChatScreen />` and type a message. The reply streams into the page.
100
101## How it works
102
103- `layout` gets `Messages`, `Interrupts`, and `Input` as components. Render them where you want them. This chat has no interrupts, so it leaves `Interrupts` out.
104- `message` gets one message plus a `Parts` component. `Parts` picks a component for each part of that message.
105- `partsComponents.text` renders text parts. `fallback` catches every part type that you did not register. Here it returns `null`, so other parts render nothing.
106- `input` reads the chat with `useChatContext()`. It comes before `layout`, so TypeScript sees that an input is registered and adds `Input` to the layout props.
107
108## Next
109
110- A tool that shows raw JSON? Read [Make a tool look right](./format-a-tool).
111- Colored code in replies? Read [Highlight markdown code](../markdown).