Open History to see the last commit
1---
2title: Debug Logging
3id: debug-logging
4order: 3
5description: "Turn on debug logging to see each chunk, middleware transform, and tool call inside TanStack AI. Pick the categories you want, or send the output to your own logger."
6keywords:
7 - tanstack ai
8 - debug
9 - logging
10 - logger
11 - pino
12 - troubleshooting
13 - chunks
14 - middleware debugging
15---
16
17Debug logging prints each chunk, middleware step, and tool argument from `chat()`. Turn it on when a run does not match what you expect.
18
19Turn on debug logging. You then see each chunk, middleware transform, and tool call in your console.
20
21## Quick setup
22
23Add `debug: true` to the call:
24
25```typescript
26import { chat } from "@tanstack/ai";
27import { openaiText } from "@tanstack/ai-openai";
28
29const stream = chat({
30 adapter: openaiText("gpt-6.1-sol"),
31 messages: [{ role: "user", content: "Hello" }],
32 threadId: "thread-1",
33 debug: true,
34});
35```
36
37Run it. Each internal event prints to the console with a `[tanstack-ai:<category>]` prefix:
38
39```
40[tanstack-ai:request] activity=chat provider=openai model=gpt-6.1-sol messages=1 tools=0 stream=true
41[tanstack-ai:agentLoop] run started
42[tanstack-ai:provider] provider=openai type=response.output_text.delta
43[tanstack-ai:output] type=TEXT_MESSAGE_CONTENT
44```
45
46## Print less
47
48Pass an object in place of `true`. Each category that you do not name stays on. Set a category to `false` to turn it off:
49
50```typescript
51import { chat } from "@tanstack/ai";
52import { openaiText } from "@tanstack/ai-openai";
53
54chat({
55 adapter: openaiText("gpt-6.1-sol"),
56 messages: [{ role: "user", content: "Hello" }],
57 threadId: "thread-1",
58 debug: { middleware: false }, // everything except middleware
59});
60```
61
62To see only some categories, set all the others to `false`. Keep `errors` on. Error logs are cheap and you need them.
63
64```typescript
65import { chat } from "@tanstack/ai";
66import { openaiText } from "@tanstack/ai-openai";
67
68chat({
69 adapter: openaiText("gpt-6.1-sol"),
70 messages: [{ role: "user", content: "Hello" }],
71 threadId: "thread-1",
72 debug: {
73 provider: true,
74 output: true,
75 middleware: false,
76 tools: false,
77 agentLoop: false,
78 config: false,
79 errors: true,
80 request: false,
81 sandbox: false,
82 },
83});
84```
85
86## Send logs to your own logger
87
88Pass a `Logger` as `logger`. All debug output then goes to it, not to `console`:
89
90```typescript
91import { chat, type Logger } from "@tanstack/ai";
92import { openaiText } from "@tanstack/ai-openai";
93import pino from "pino";
94
95const pinoLogger = pino();
96const logger: Logger = {
97 debug: (msg, meta) => pinoLogger.debug(meta, msg),
98 info: (msg, meta) => pinoLogger.info(meta, msg),
99 warn: (msg, meta) => pinoLogger.warn(meta, msg),
100 error: (msg, meta) => pinoLogger.error(meta, msg),
101};
102
103chat({
104 adapter: openaiText("gpt-6.1-sol"),
105 messages: [{ role: "user", content: "Hello" }],
106 threadId: "thread-1",
107 debug: { logger }, // all categories on, sent to pino
108});
109```
110
111The default logger is `ConsoleLogger`. You can import it to wrap it:
112
113```typescript
114import { ConsoleLogger } from "@tanstack/ai";
115```
116
117## Categories
118
119| Category | Logs | Applies to |
120|----------|------|------------|
121| `request` | The outgoing call to a provider (model, message count, tool count) | All activities |
122| `provider` | Each raw chunk or frame from a provider SDK | Streaming activities (`chat`, `realtime`, and streaming `generateAudio`, `generateSpeech`, `generateTranscription`) |
123| `output` | Each chunk or result that goes to the caller | All activities |
124| `middleware` | Inputs and outputs around each middleware hook | `chat()` only |
125| `tools` | Before and after each tool call | `chat()` only |
126| `agentLoop` | Agent-loop iterations and phase changes | `chat()` only |
127| `config` | Config changes that middleware `onConfig` hooks return | `chat()` only |
128| `errors` | Each caught error in the pipeline | All activities |
129| `sandbox` | Sandbox internals: watcher start and stop, file events, sandbox hooks, and lifecycle changes | `chat()` only |
130
131Building a middleware? `debug: { middleware: true }` shows the chunks that go through it, with no logging middleware of your own. See [Middleware](./middleware) to write one.
132
133## Advanced
134
135### Errors are always logged
136
137The logger gets errors even when you leave out `debug`:
138
139```typescript
140import { chat } from "@tanstack/ai";
141import { openaiText } from "@tanstack/ai-openai";
142
143// On a failure, this still prints [tanstack-ai:errors] ...
144chat({
145 adapter: openaiText("gpt-6.1-sol"),
146 messages: [{ role: "user", content: "Hello" }],
147 threadId: "thread-1",
148});
149```
150
151To turn off all output, errors included, set `debug: false` or `debug: { errors: false }`.
152
153The logger is an extra output. Errors also always reach the caller, as thrown exceptions or `RUN_ERROR` stream chunks.
154
155### A failing logger does not break the call
156
157The library calls your `Logger` inside a try/catch. If your logger throws, the library drops the exception. Examples are a `JSON.stringify` on a cyclic `meta`, a transport that rejects synchronously, or a wrong `this` binding.
158
159This way, a logger failure never hides the real error that caused the log call, for example a provider SDK failure in the chat stream. You do not see the log line. The pipeline error still reaches you through thrown exceptions and `RUN_ERROR` chunks.
160
161To know when your own logger fails, add a guard in your implementation:
162
163```typescript
164import { type Logger } from "@tanstack/ai";
165import pino from "pino";
166
167const pinoLogger = pino();
168const logger: Logger = {
169 debug: (msg, meta) => {
170 try {
171 pinoLogger.debug(meta, msg);
172 } catch (err) {
173 // Send it to the place where you track infrastructure errors.
174 process.stderr.write(`logger failed: ${String(err)}\n`);
175 }
176 },
177 info: (msg, meta) => pinoLogger.info(meta, msg),
178 warn: (msg, meta) => pinoLogger.warn(meta, msg),
179 error: (msg, meta) => pinoLogger.error(meta, msg),
180};
181```
182
183### Other activities
184
185The same `debug` option works on each activity:
186
187```typescript
188import {
189 summarize,
190 generateImage,
191 generateSpeech,
192 generateAudio,
193 generateTranscription,
194 type Logger,
195} from "@tanstack/ai";
196import { adapter } from "./server";
197import { logger } from "./logger";
198
199const threadId = "thread-1";
200const text = "Long article to summarize...";
201const audio = new File([""], "recording.mp3", { type: "audio/mpeg" });
202
203summarize({ adapter, text, debug: true, threadId });
204generateImage({ adapter, prompt: "a cat", debug: { logger }, threadId });
205generateSpeech({ adapter, text, debug: { request: true }, threadId });
206generateAudio({ adapter, prompt: "ambient piano", debug: true, threadId });
207generateTranscription({ adapter, audio, debug: { provider: true }, threadId });
208```
209
210When you stream `generateAudio`, `generateSpeech`, or `generateTranscription` (`stream: true`):
211
212- `provider` shows the raw SDK chunks.
213- `output` shows the AG-UI chunks that go to the caller.
214
215If a media pipeline seems stuck, or the bytes are not what you expect, read these two categories.
216
217The chat-only categories (`middleware`, `tools`, `agentLoop`, `config`) never fire for these activities. Those parts do not exist in their pipelines.
218
219For traces and metrics in production, use [OpenTelemetry](./otel).