<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/tanstack-ai-just-got-middleware-and-it-changes-everything-wvayy3lkl" -->

---
title: TanStack AI Just Got Middleware — And It Changes Everything
description: TanStack AI has shipped a first-class, composable middleware system for its chat() function, exposed via a new @tanstack/ai/middlewares subpath. Middleware are...
canonical: https://daily.dev/posts/tanstack-ai-just-got-middleware-and-it-changes-everything-wvayy3lkl
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: TanStack AI Just Got Middleware — And It Changes Everything | daily.dev
og:description: TanStack AI has shipped a first-class, composable middleware system for its chat() function, exposed via a new @tanstack/ai/middlewares subpath. Middleware are...
og:url: https://daily.dev/posts/tanstack-ai-just-got-middleware-and-it-changes-everything-wvayy3lkl
og:image: https://api.daily.dev/og/posts/wvAYY3LKl.png
og:image:alt: TanStack AI Just Got Middleware — And It Changes Everything
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# TanStack AI Just Got Middleware — And It Changes Everything

**[TanStack](https://daily.dev/sources/tanstack-blog)** · 10 min read · 0 upvotes · 0 comments

## Summary

TanStack AI has shipped a first-class, composable middleware system for its chat() function, exposed via a new @tanstack/ai/middlewares subpath. Middleware are plain objects implementing hooks like onConfig, onChunk, onBeforeToolCall/onAfterToolCall, onUsage, onFinish/onAbort/onError, giving fine-grained control over the chat lifecycle. Two built-in middlewares ship out of the box: toolCacheMiddleware (with pluggable storage, custom cache keys, TTL/LRU eviction) and contentGuardMiddleware (regex or function-based PII/content redaction with buffered or blocking strategies). Composition semantics differ by hook — piped, first-win, or sequential — allowing developers to layer concerns like tenant-specific prompts, dangerous tool blocking, content redaction, caching, token budgets, and analytics in a single explicit array. Middleware events also integrate with TanStack DevTools via new Iteration Timeline and Iteration Card components for real-time pipeline visualization.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://tanstack.com/blog/tanstack-ai-middleware>

## Questions this post answers

### How do I add caching, logging, or content filtering to a TanStack AI chat() call without wrapping the function myself?

TanStack AI now provides a first-class middleware system for chat() where middleware are plain objects implementing hooks like onConfig, onChunk, onBeforeToolCall, onAfterToolCall, onUsage, and onFinish/onAbort/onError. You pass an array of middleware objects to the middleware option on chat(), and they compose in order with piped, first-win, or sequential semantics depending on the hook.

_Developers wiring logging, caching, and guardrails into AI chat pipelines can track framework updates like this on daily.dev._

### How does TanStack AI's toolCacheMiddleware avoid calling the same tool multiple times in one conversation?

toolCacheMiddleware uses the onBeforeToolCall hook to check a cache and returns a skip decision with the cached result on a hit, so the tool never executes again; on a miss, onAfterToolCall stores the result after execution. Failed calls are never cached, and options include ttl, maxSize for LRU eviction, toolNames filtering, custom cache keys via keyFn, and pluggable storage (e.g. Redis) via getItem/setItem/deleteItem.

_Teams optimizing AI agent tool costs can follow practical caching patterns like this on daily.dev._

### How can I redact PII like SSNs or API keys from a streamed AI chat response before it reaches the client?

contentGuardMiddleware in TanStack AI lets you define regex or function-based rules with a pattern and replacement (or a custom fn) to redact matches such as SSNs or API keys from streamed chunks. It supports a buffered strategy with a configurable bufferSize to catch matches spanning chunk boundaries, and a blockOnMatch option to drop entire chunks instead of replacing text when a rule matches.

_Developers hardening AI chat outputs against PII leaks can keep up with tooling like this on daily.dev._

## Similar posts on daily.dev

- [TanStack Releases Framework Agnostic AI Toolkit](https://daily.dev/posts/tanstack-releases-framework-agnostic-ai-toolkit-j747yitl6) · InfoQ · 1 upvotes · 0 comments
- [Introducing TanStack Start Middleware – Frontend Masters Blog](https://daily.dev/posts/introducing-tanstack-start-middleware-frontend-masters-blog-b3wqhesk9) · Frontend Masters · 8 upvotes · 0 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#observability](https://daily.dev/tags/observability)

[View this post on daily.dev](https://daily.dev/posts/tanstack-ai-just-got-middleware-and-it-changes-everything-wvayy3lkl)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"TanStack AI Just Got Middleware — And It Changes Everything","url":"https://daily.dev/posts/tanstack-ai-just-got-middleware-and-it-changes-everything-wvayy3lkl","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/tanstack-ai-just-got-middleware-and-it-changes-everything-wvayy3lkl"},"datePublished":"2026-08-23T12:22:22.690Z","dateModified":"2026-08-23T12:54:38.036Z","description":"TanStack AI has shipped a first-class, composable middleware system for its chat() function, exposed via a new @tanstack/ai/middlewares subpath. Middleware are...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2603d35ce48d9a6c3ae02cb4aefac826?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2603d35ce48d9a6c3ae02cb4aefac826?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"TanStack","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"TanStack","logo":"https://media.daily.dev/image/upload/s--TVIrvIT7--/f_auto,q_auto/v1787487634/logos/tanstack-blog?_a=BAMAMicg0","url":"https://daily.dev/sources/tanstack-blog"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/tanstack-ai-just-got-middleware-and-it-changes-everything-wvayy3lkl","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript,observability","timeRequired":"PT10M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"TanStack","item":"https://daily.dev/sources/tanstack-blog"},{"@type":"ListItem","position":3,"name":"TanStack AI Just Got Middleware — And It Changes Everything"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/tanstack-ai-just-got-middleware-and-it-changes-everything-wvayy3lkl#faq","mainEntity":[{"@type":"Question","name":"How do I add caching, logging, or content filtering to a TanStack AI chat() call without wrapping the function myself?","acceptedAnswer":{"@type":"Answer","text":"TanStack AI now provides a first-class middleware system for chat() where middleware are plain objects implementing hooks like onConfig, onChunk, onBeforeToolCall, onAfterToolCall, onUsage, and onFinish/onAbort/onError. You pass an array of middleware objects to the middleware option on chat(), and they compose in order with piped, first-win, or sequential semantics depending on the hook. Developers wiring logging, caching, and guardrails into AI chat pipelines can track framework updates like this on daily.dev."}},{"@type":"Question","name":"How does TanStack AI's toolCacheMiddleware avoid calling the same tool multiple times in one conversation?","acceptedAnswer":{"@type":"Answer","text":"toolCacheMiddleware uses the onBeforeToolCall hook to check a cache and returns a skip decision with the cached result on a hit, so the tool never executes again; on a miss, onAfterToolCall stores the result after execution. Failed calls are never cached, and options include ttl, maxSize for LRU eviction, toolNames filtering, custom cache keys via keyFn, and pluggable storage (e.g. Redis) via getItem/setItem/deleteItem. Teams optimizing AI agent tool costs can follow practical caching patterns like this on daily.dev."}},{"@type":"Question","name":"How can I redact PII like SSNs or API keys from a streamed AI chat response before it reaches the client?","acceptedAnswer":{"@type":"Answer","text":"contentGuardMiddleware in TanStack AI lets you define regex or function-based rules with a pattern and replacement (or a custom fn) to redact matches such as SSNs or API keys from streamed chunks. It supports a buffered strategy with a configurable bufferSize to catch matches spanning chunk boundaries, and a blockOnMatch option to drop entire chunks instead of replacing text when a rule matches. Developers hardening AI chat outputs against PII leaks can keep up with tooling like this on daily.dev."}}]}
```

