ai-core/middleware

Instrument TanStack AI chat lifecycle hooks with middleware for analytics and tool decisions.

3.0k|295|Updated Oct 8, 2025
One-click install
npx skills add https://github.com/TanStack/ai --skill ai-core-middleware
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ai-core/middleware
Source: https://github.com/TanStack/ai/tree/main/packages/typescript/ai/skills/ai-core/middleware
Command: npx skills add https://github.com/TanStack/ai --skill ai-core-middleware

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill ensures every chat lifecycle hook in TanStack AI emits telemetry, enforces tool behavior, and surfaces errors without manually weaving instrumentation into each run.

Core Features & Use Cases

  • Lifecycle hook coverage: onConfig, onStart, onChunk, tool call, usage, and terminal hooks provide observability at every phase of a chat.
  • Tool management and caching: onBeforeToolCall and onAfterToolCall empower guards, argument transforms, and caching decisions before the conversation continues.
  • Resilient analytics: ctx.defer and explicit error handling let logging, tracking, and deferred side effects run without blocking or aborting the stream, ideal for SaaS observability pipelines.

Quick Start

Add middleware entries that log request IDs, cache tool results, and report errors before passing the configuration to chat.

Frequently Asked Questions about ai-core/middleware

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add observability and usage tracking to TanStack AI chat lifecycle hooks?

You add observability to TanStack AI chat hooks by applying middleware arrays that inspect ChatMiddlewareContext to automatically log request IDs, track usage, and report errors across init, streaming, and terminal phases.

Can I intercept and cache tool calls in a TanStack AI chat stream?

Yes, onBeforeToolCall and onAfterToolCall middleware hooks intercept tool invocations to enforce guards, transform arguments, and apply tool-caching decisions before the conversation stream continues.

How to log analytics from an AI chat stream without blocking the response?

Use ctx.defer within your middleware implementations to execute resilient analytics logging and deferred side effects asynchronously. This ensures tracking pipelines run without disrupting or aborting the main chat stream.

Does TanStack middleware support short-circuiting tool decisions during a chat invocation?

Yes, middleware implementations can return short-circuit decisions from lifecycle hooks to guard tools and control execution flow, maintaining strict observability and behavioral enforcement throughout the chat invocation.

What is the best way to handle errors and telemetry in AI chat lifecycle hooks?

The best way is using middleware entries that provide explicit error handling and lifecycle hook coverage at every chat phase. This surfaces errors and emits telemetry without requiring manual instrumentation woven into each run.