ai-core/middleware

ai-core/middleware is a skill for Claude Code from TanStack/ai. It costs 86 tokens per session (8,891 once invoked), scanned A, original, MIT.

Middleware hooks for observing and changing stages of an AI chat request, such as startup, streamed chunks, tool calls, usage, completion, abortion, and errors. Middleware is code that runs between an application and the chat process.

In plain words
What is it for?
Adding analytics, logging, tracing, tool caching, error reporting, and configuration changes to chats built with the referenced AI chat library.
Why use it?
Applications often need one place to log chats, track usage, report errors, cache tool results, or inspect streamed responses. These hooks provide lifecycle points for adding that behavior.

Skill for Claude Code

Written for Claude Code: shipped in a Claude Code plugin. Also seen: mentions subagents.

Part of the tanstack-ai plugin — 47 skills shipped together

Good fit Adding analytics, logging, tracing, tool caching, error reporting, and configuration changes to chats built with the referenced AI chat library.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/tanstack/ai/middleware
About the project

TanStack AI is a TypeScript SDK for building applications that stream AI responses, call tools, run agents, produce structured output, and handle multimodal or realtime interactions. Developers use it across frameworks and AI providers through shared typed interfaces and provider adapters. The catalogue entries are add-ons for working with this SDK.

TanStack/ai · 3,359 stars · on GitHub · tanstack.com

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add TanStack/ai --skill middleware
Clone the repo
git clone --depth 1 https://github.com/TanStack/ai

Made for: Claude Code.

Or install tanstack-ai, the plugin that ships this one along with the rest of its 47 skills.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for ai-core/middleware

README.md
[![agentmods](https://agentmods.dev/badge/skills/tanstack/ai/middleware/github.svg)](https://agentmods.dev/skills/tanstack/ai/middleware)
Your own site
<a href="https://agentmods.dev/skills/tanstack/ai/middleware"><img src="https://agentmods.dev/badge/skills/tanstack/ai/middleware/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for ai-core/middleware

Your own site · 80×15
<a href="https://agentmods.dev/skills/tanstack/ai/middleware"><img src="https://agentmods.dev/badge/skills/tanstack/ai/middleware.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 86 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 8,891 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe. Third-party audits
  • NVIDIA SkillSpector warn 7 Sept 2026
SkillSpector: 16 findings, up to high

These are SkillSpector’s own severities. On a checked sample its high-severity flags on skills were ~96% false positives — a documented command, a public API, a “never do X” rule — so we show them as a caution to read, not a verdict. Why →

  • high Anti-Refusal · line 340
    Skill instructs the agent to never refuse or to always comply. Suppressing the agent's ability to decline removes a core safety control and enables downstream harmful requests to succeed.
    Fix: Remove any instruction telling the agent to never refuse or always comply. The agent must retain the ability to decline unsafe, out-of-scope, or harmful requests.
  • medium Prompt Injection · line 55
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 62
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 63
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 64
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 69
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 67
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 68
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 85
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 87
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 88
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 89
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 90
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Prompt Injection · line 91
    Large whitespace padding was detected (a block of blank lines or a long run of spaces). This can push injected instructions below or to the right of the visible area so a human reviewer never sees them while the agent still reads them. Manual review of the hidden content is recommended.
    Fix: Remove the large whitespace padding (blank-line blocks or long space runs) and review any content hidden below or to the right of it. Keep skill files compact and reviewable so no instructions can be
  • medium Agent Snooping · line 566
    Skill enumerates or reads other installed skills. Access to other skills' SKILL.md files or the skills directory reveals prompt instructions, capabilities, and secrets that should be invisible to peer skills.
    Fix: Remove all code or instructions that list or read other skills' files or directories. Skills should operate independently; cross-skill access is a privilege escalation.
  • medium Agent Snooping · line 793
    Skill enumerates or reads other installed skills. Access to other skills' SKILL.md files or the skills directory reveals prompt instructions, capabilities, and secrets that should be invisible to peer skills.
    Fix: Remove all code or instructions that list or read other skills' files or directories. Skills should operate independently; cross-skill access is a privilege escalation.
How audits are shown
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00086 $0.08891
Opus 5.5 $0.00034 $0.03556
Sonnet 5.5 $0.00017 $0.01778
Haiku 4.5 $0.00009 $0.00889

Measured 3d ago against content hash b5a76c2cbd08, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

ai-core/middleware scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

packages/ai/skills/ai-core/middleware/SKILL.md · 907 lines

How it starts

The opening of the file, as written. The whole thing — 907 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Middleware

Dependency note: This skill builds on ai-core. Read it first for critical rules.

Setup — Analytics Tracking Middleware

import { chat, toServerSentEventsResponse } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'
import { trackAnalytics, reportError } from './analytics'

export async function POST(request: Request) {
  const { messages, threadId } = await request.json()

  const stream = chat({
    adapter: openaiText('gpt-5.5'),
    messages,
    threadId,
    middleware: [
      {
        onStart: (ctx) => {
          console.log('Chat started:', ctx.model)
        },
        onFinish: (ctx, info) => {
          trackAnalytics({ model: ctx.model, tokens: info.usage?.totalTokens })
        },
        onError: (ctx, info) => {
          reportError(info.error)
        },
      },
    ],
  })

  return toServerSentEventsResponse(stream)
}

Hooks Reference

Every hook receives a ChatMiddlewareContext as its first argument, which provides requestId, streamId, phase, iteration, chunkIndex, callMetadata, model, provider, signal, abort(), defer(), and more. parentRunId names the run this one continues. subagentRunId is set only inside a subagent and names its card.

Hook When Second Argument
onConfig Once at startup (init) + once per iteration (beforeModel) + once at a separate-finalization boundary ChatMiddlewareConfig (return partial to merge)
onStructuredOutputConfig Once at the separate-finalization boundary StructuredOutputMiddlewareConfig (return partial)
onStart Once after initial onConfig none
onIteration Start of each agent loop iteration IterationInfo
onShouldContinue Whether to start another agent-loop iteration (AND with strategy; false stops) AgentLoopState
onChunk Every streamed chunk StreamChunk (return void/chunk/chunk[]/null)
onBeforeToolCall Before each tool executes ToolCallHookContext (return decision or void)
onAfterToolCall After each tool executes AfterToolCallInfo
onToolPhaseComplete After all tool calls in an iteration ToolPhaseCompleteInfo
onUsage When RUN_FINISHED includes usage data UsageInfo
onFinish Run completed normally FinishInfo
onAbort Run was aborted, including a consumer break or return() AbortInfo
onError Unhandled error occurred ErrorInfo

Read the full file on GitHub · 907 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago Changed b5a76c2cbd08
  2. 24d ago Changed · +5 lines 2579c6f26dd5
  3. 1mo ago Changed · +3 lines 621882e064f5
  4. 2mo ago Changed · +105 lines aa2dffb4c00b
  5. 2mo ago First seen · 794 lines · 86 tokens per session scan A 13d23d966f6a

Subscribe to this mod's changes

ai-core/middleware is a skill published in the GitHub repository TanStack/ai (3,359 stars, last pushed yesterday), licensed MIT. It adds 86 tokens to every session and 8,891 once invoked, about $0.0003 per session on Opus 5.5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.

Related

ai-core/middleware compared with skills from other repositories

ai-core/middleware next to 6 skills from other repositories
ModTypeStarsLast pushLicenceScanTokensWorks with
ai-core/middlewareTanStack/aiSkill3.4kyesterdayMITA86Claude Code
claude-apiasgeirtj/system_prompts_leaksSkill71kyesterdayCC0-1.0A317Claude Code, Codex
non-storybookasgeirtj/system_prompts_leaksSkill71kyesterdayCC0-1.0A0Claude Code, Codex
setup-writing-styleasgeirtj/system_prompts_leaksSkill71kyesterdayCC0-1.0A97Claude Code, Codex
shoppingasgeirtj/system_prompts_leaksSkill71kyesterdayCC0-1.0A0Claude Code, Codex
instagramasgeirtj/system_prompts_leaksSkill71kyesterdayCC0-1.0A57Claude Code, Codex
pptxasgeirtj/system_prompts_leaksSkill71kyesterdayCC0-1.0A217Claude Code, Codex