Observability
Once the chat is live, you'll want to know what it costs, where a slow answer spent its time, and when something breaks. askdare keeps no database, so it hands each turn to your code instead, through two hooks of createChatHandler:
| Hook | Receives | For |
|---|---|---|
onUsage | Every turn: tokens, model, timings, tool calls, outcome | Costs, dashboards, tracing |
onError | Server errors and errors that end an answer | Error monitoring, such as Sentry |
Both may be async. askdare calls them after the answer has streamed, keeps the function alive until they finish, and logs anything they throw by its error's name, so a broken hook never breaks the chat. Neither receives message text: reports carry counters and pseudonyms.
Visitors' ratings of answers reach a third hook, onFeedback: see Answer ratings. A fourth, onTranscript, hands you each turn's message and answer to keep your own transcripts; it is the only hook with message content, and off unless you set it.
For analytics on the page itself, such as GA4 or a GTM dataLayer, listen to the chat's events in the browser.
Costs per turn
// app/api/chat/[...path]/route.ts
import { createChatHandler } from '@boldare/askdare/next';
import { bots } from '../../../../askdare.config';
export const maxDuration = 300;
export const { GET, POST, DELETE } = createChatHandler({
bots,
onUsage: async ({ bot, model, usage, outcome }) => {
await metrics.increment('chat.turns', { bot, model, outcome });
await metrics.increment('chat.input_tokens', usage.inputTokens, { bot, model });
await metrics.increment('chat.output_tokens', usage.outputTokens, { bot, model });
},
});Multiply the counters by your model's prices to get money. Cached input is part of the input and usually costs less; reasoning tokens are part of the output. conversation is a pseudonym that stays the same for every turn of a conversation, so you can sum a conversation's cost without knowing whose it was. Costs explains what drives them.
Tracing with OpenTelemetry
@boldare/askdare-otel turns each report into OpenTelemetry spans, with the attributes of the GenAI semantic conventions:
npm install @boldare/askdare-otel @opentelemetry/apiimport { otelUsage } from '@boldare/askdare-otel';
export const { GET, POST, DELETE } = createChatHandler({ bots, onUsage: otelUsage() });Each turn becomes a chat <model> span with the token counts, the finish reason and the conversation's pseudonym. Each tool call becomes an execute_tool <name> span inside it, marked as an error when it failed or timed out. A turn with several model calls (rounds of tool calls) is one chat span with their usage summed.
A tool span's gen_ai.tool.type follows the call's kind, and askdare.tool.kind keeps the kind itself, so you can still tell an httpTool from an MCP tool:
kind | gen_ai.tool.type |
|---|---|
function (defineTool, search_knowledge) | function |
http (httpTool) | extension |
mcp | extension |
web_search | datastore |
An httpTool call that got an answer also has http.response.status_code, the remote server's status code. A tool span never carries the URL, the headers, the arguments or the result.
The spans go to whatever tracer provider your app registers. In Next.js that is instrumentation.ts, for example with @vercel/otel:
// instrumentation.ts
import { registerOTel } from '@vercel/otel';
export function register() {
registerOTel({ serviceName: 'my-site' });
}Pass otelUsage({ tracer }) to record with a tracer of your own.
The spans are recorded after the turn ends, with its real start and end times, in whatever trace context is active then. With Next.js instrumentation that is the request's trace; otherwise each turn starts a trace of its own.
The GenAI conventions are still in development. When they rename an attribute, @boldare/askdare-otel follows in a minor release and says so in its changelog.
Costs and ratings per prompt version
Every report names the prompt version that answered, in version: onUsage, onTranscript, onFeedback (the version that wrote the rated answer), onHandoff and onLead. It is the bot's version, default unless you name it, or the one an experiment gave the conversation. Group by it next to bot to compare versions:
onUsage: async ({ bot, version, usage }) => {
await metrics.increment('chat.output_tokens', usage.outputTokens, { bot, version });
},@boldare/askdare-otel doesn't put the version on its spans in 0.8: the GenAI conventions have no attribute for it.
What the reports contain
The full list is in Configuration. In short:
- Counted: input, cached input, cache-write input, output and reasoning tokens; model calls; tool calls with their durations and outcomes.
- Named: the bot, the model and the prompt version.
- Identified by pseudonyms: the conversation and the visitor (or the signed-in user), as HMACs that change when
CHAT_SECRETrotates. - Never included: message text, tool arguments or results, file names, IP addresses, raw conversation or visitor IDs, and for remote calls the URL, the headers and the server's answer.
Each tool call in tools says what it reached in kind (since 0.8): function for a defineTool or a built-in tool such as search_knowledge, http for an httpTool, mcp for an MCP server's tool and web_search for a web search. An http call also has status, the code your server answered with, also when the call failed (a 404, or a 302 that wasn't followed); it is absent when the request got no answer (a timeout, a network error, a value that couldn't go in the URL, or a call a limit refused). So you can watch your own API from the chat's side:
onUsage: async ({ bot, tools }) => {
for (const call of tools.filter((tool) => tool.kind === 'http')) {
await metrics.increment('chat.api_calls', { bot, tool: call.name, status: String(call.status ?? 'none') });
}
},A turn that breaks off mid-answer reports the usage askdare received, which can be a little less than OpenAI bills.
Events in the browser
The page itself can hear what happens in the chat (opened, message sent, answer received, answer rated) for analytics such as GA4 or a GTM dataLayer: askdare('on', type, listener) with the script tag, or onEvent on Root in React. The events carry metadata only. See Chat events.