Open History to see the last commit
1---
2title: Debug Logging
3id: debug-logging
4order: 3
5description: "Turn on structured, category-toggleable debug logging to see every chunk, middleware transform, and tool call inside TanStack AI."
6keywords:
7 - tanstack ai
8 - debug
9 - logging
10 - logger
11 - pino
12 - troubleshooting
13 - chunks
14 - middleware debugging
15---
16
17You have a `chat()` that isn't behaving as expected — a missing chunk, a middleware that doesn't seem to fire, a tool call with wrong args. By the end of this guide, you'll have turned on debug logging and will see every chunk, middleware transform, and tool call flowing through your call.
18
19## Turn it on
20
21Add `debug: true` to any activity call:
22
23```typescript
24import { chat } from "@tanstack/ai";
25import { openaiText } from "@tanstack/ai-openai";
26
27const stream = chat({
28 adapter: openaiText("gpt-5.5"),
29 messages: [{ role: "user", content: "Hello" }],
30 debug: true,
31});
32```
33
34Every internal event now prints to the console with a `[tanstack-ai:<category>]` prefix:
35
36```
37[tanstack-ai:request] activity=chat provider=openai model=gpt-5.5 messages=1 tools=0 stream=true
38[tanstack-ai:agentLoop] run started
39[tanstack-ai:provider] provider=openai type=response.output_text.delta
40[tanstack-ai:output] type=TEXT_MESSAGE_CONTENT
41...
42```
43
44## Narrow what's printed
45
46Pass a `DebugConfig` object instead of `true`. Every unspecified category defaults to `true`, so toggle by setting specific flags to `false`:
47
48```typescript
49import { chat } from "@tanstack/ai";
50import { openaiText } from "@tanstack/ai-openai";
51
52chat({
53 adapter: openaiText("gpt-5.5"),
54 messages: [{ role: "user", content: "Hello" }],
55 debug: { middleware: false }, // everything except middleware
56});
57```
58
59If you want to see ONLY a specific set of categories, set the rest to `false` explicitly. Errors default to `true` — keep them on unless you really want total silence:
60
61```typescript
62import { chat } from "@tanstack/ai";
63import { openaiText } from "@tanstack/ai-openai";
64
65chat({
66 adapter: openaiText("gpt-5.5"),
67 messages: [{ role: "user", content: "Hello" }],
68 debug: {
69 provider: true,
70 output: true,
71 middleware: false,
72 tools: false,
73 agentLoop: false,
74 config: false,
75 errors: true, // keep errors on — they're cheap and important
76 request: false,
77 },
78});
79```
80
81## Pipe into your own logger
82
83Pass a `Logger` implementation and all debug output flows through it instead of `console`:
84
85```typescript
86import { chat, type Logger } from "@tanstack/ai";
87import { openaiText } from "@tanstack/ai-openai";
88import pino from "pino";
89import { messages } from "./server";
90
91const pinoLogger = pino();
92const logger: Logger = {
93 debug: (msg, meta) => pinoLogger.debug(meta, msg),
94 info: (msg, meta) => pinoLogger.info(meta, msg),
95 warn: (msg, meta) => pinoLogger.warn(meta, msg),
96 error: (msg, meta) => pinoLogger.error(meta, msg),
97};
98
99chat({
100 adapter: openaiText("gpt-5.5"),
101 messages,
102 debug: { logger }, // all categories on, piped to pino
103});
104```
105
106The default logger is exported as `ConsoleLogger` if you want to wrap it:
107
108```typescript
109import { ConsoleLogger } from "@tanstack/ai";
110```
111
112### Your `Logger` is wrapped in a try/catch
113
114If your `Logger` implementation throws — a cyclic-meta `JSON.stringify`, a transport that rejects synchronously, a typo in a bound `this` — the exception is swallowed so it never masks the real error that triggered the log call (for example, a provider SDK failure inside the chat stream). You won't see the log line, but the pipeline error still surfaces through thrown exceptions and `RUN_ERROR` chunks.
115
116If you need to know when your own logger is failing, guard inside your implementation:
117
118```typescript
119import { type Logger } from "@tanstack/ai";
120import pino from "pino";
121
122const pinoLogger = pino();
123const logger: Logger = {
124 debug: (msg, meta) => {
125 try {
126 pinoLogger.debug(meta, msg);
127 } catch (err) {
128 // surface to wherever you track infra errors
129 process.stderr.write(`logger failed: ${String(err)}\n`);
130 }
131 },
132 info: (msg, meta) => pinoLogger.info(meta, msg),
133 warn: (msg, meta) => pinoLogger.warn(meta, msg),
134 error: (msg, meta) => pinoLogger.error(meta, msg),
135};
136```
137
138## Categories reference
139
140| Category | Logs | Applies to |
141|----------|------|------------|
142| `request` | Outgoing call to a provider (model, message count, tool count) | All activities |
143| `provider` | Every raw chunk/frame received from a provider SDK | Streaming activities (`chat`, `realtime`, and streaming `generateAudio`/`generateSpeech`/`generateTranscription`) |
144| `output` | Every chunk or result yielded to the caller | All activities |
145| `middleware` | Inputs and outputs around every middleware hook | `chat()` only |
146| `tools` | Before/after tool call execution | `chat()` only |
147| `agentLoop` | Agent-loop iterations and phase transitions | `chat()` only |
148| `config` | Config transforms returned by middleware `onConfig` hooks | `chat()` only |
149| `errors` | Every caught error anywhere in the pipeline | All activities |
150
151## Errors are always logged
152
153Errors flow through the logger unconditionally — even when you omit `debug`:
154
155```typescript
156import { chat } from "@tanstack/ai";
157import { adapter } from "./server";
158
159chat({ adapter, messages: [{ role: "user", content: "Hello" }] }); // still prints [tanstack-ai:errors] ... on failure
160```
161
162To fully silence (including errors), set `debug: false` or `debug: { errors: false }`. Errors also always reach the caller via thrown exceptions or `RUN_ERROR` stream chunks — the logger is additive, not the only surface.
163
164## Non-chat activities
165
166The same `debug` option works on every activity:
167
168```typescript
169import {
170 summarize,
171 generateImage,
172 generateSpeech,
173 generateAudio,
174 generateTranscription,
175 type Logger,
176} from "@tanstack/ai";
177import { adapter } from "./server";
178import { logger } from "./logger";
179
180const text = "Long article to summarize...";
181const audio = new File([""], "recording.mp3", { type: "audio/mpeg" });
182
183summarize({ adapter, text, debug: true });
184generateImage({ adapter, prompt: "a cat", debug: { logger } });
185generateSpeech({ adapter, text, debug: { request: true } });
186generateAudio({ adapter, prompt: "ambient piano", debug: true });
187generateTranscription({ adapter, audio, debug: { provider: true } });
188```
189
190When streaming any of these (`generateAudio`, `generateSpeech`, `generateTranscription` with `stream: true`), the `provider` category emits the raw SDK chunks and `output` emits the AG-UI-shaped chunks yielded to the caller — useful when a media pipeline looks stuck or the bytes arriving don't match what you expected.
191
192The chat-only categories (`middleware`, `tools`, `agentLoop`, `config`) simply never fire for these activities because those concepts don't exist in their pipelines.
193
194## Related
195
196If you're building middleware and want to see chunks flow through it, `debug: { middleware: true }` is faster than writing a logging middleware. See [Middleware](./middleware) for writing your own middleware.