middleware

middleware is a skill for Claude Code from RailtownAI/railtracks. It costs 45 tokens per session (2,310 once invoked), scanned A, original, MIT.

A guide to adding middleware to Railtracks agents and nodes. Middleware is code that runs before, after, or around another operation so it can change inputs or results and handle failures.

In plain words
What is it for?
It helps add processing around node calls and language-model calls, including input preparation, result changes, and failure handling.
Why use it?
It removes guesswork about which wrapper or callback to use and where each one runs. It also clarifies which functions must be asynchronous and which can be synchronous.

Skill for Claude Code

Written for Claude Code: argument-hint in frontmatter.

Good fit It helps add processing around node calls and language-model calls, including input preparation, result changes, and failure handling.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/railtownai/railtracks/middleware
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 RailtownAI/railtracks --skill middleware
Clone the repo
git clone --depth 1 https://github.com/RailtownAI/railtracks

Made for: Claude Code.

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 middleware

README.md
[![agentmods](https://agentmods.dev/badge/skills/railtownai/railtracks/middleware/github.svg)](https://agentmods.dev/skills/railtownai/railtracks/middleware)
Your own site
<a href="https://agentmods.dev/skills/railtownai/railtracks/middleware"><img src="https://agentmods.dev/badge/skills/railtownai/railtracks/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 middleware

Your own site · 80×15
<a href="https://agentmods.dev/skills/railtownai/railtracks/middleware"><img src="https://agentmods.dev/badge/skills/railtownai/railtracks/middleware.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 45 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,310 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.
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.00045 $0.02310
Opus 5.5 $0.00018 $0.00924
Sonnet 5.5 $0.00009 $0.00462
Haiku 4.5 $0.00005 $0.00231

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

Security

Grade A, and why

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 2d 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/railtracks/src/railtracks/cli/skills/middleware/SKILL.md · 143 lines

How it starts

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

Add Middleware to a Railtracks Agent or Node

The user wants to add middleware to a railtracks node/agent: $ARGUMENTS

Quick reference

Decorator Signature Slot Fires on failure?
rt.wrap_node async fn(call, *args, **kwargs) -> result middleware= or model_middleware= Yes — you control the try/except
rt.wrap_llm async fn(llm_call, message_history, schema, tools) -> Response model_middleware= only Yes
rt.pre_llm fn(message_history, schema, tools) -> (message_history, schema, tools) (sync or async) model_middleware= only N/A (runs before the call)
rt.post_llm fn(response) -> response (sync or async) model_middleware= only No — skipped on exception
rt.post_node fn(result) -> result (sync or async) middleware= or model_middleware= No — skipped on exception
  • All five accept optional keyword-only name= and work bare or called: @rt.pre_llm, @rt.post_node(name="print_after").
  • wrap_node/wrap_llm require async def (TypeError otherwise). pre_llm/post_llm/post_node accept sync or async.
  • middleware= wraps the whole node call (an agent's full tool-calling loop, including every tool node it invokes). model_middleware= wraps one raw LLM call inside that loop.
  • Attach at creation — rt.agent_node(..., middleware=[...], model_middleware=[...]) — or after the fact without mutating the original — rt.couple(node, middleware=[...], model_middleware=[...]).
  • List order = wrapping order: middleware=[A, B, C] → A wraps B wraps C wraps the node. rt.couple-ing more onto an already-middlewared node adds the new ones innermost; existing middleware stays outermost.

Best practices

1. Exception handling & retries

  • pre_llm, post_llm, post_node only run on success — an exception skips them entirely. If you need to react to failure (cleanup, translation, fallback), use wrap_node/wrap_llm and put your own try/except around await call(...).
  • Where you put a retry changes what it re-executes:
    • model_middleware= retry only re-issues the raw LLM call — safe by default, no side effects beyond the model provider request.
    • middleware= retry on an agent re-runs the entire node, including every tool call that already succeeded in that attempt. Only retry at this slot if the tools involved are safe to re-run (idempotent, or guarded by an idempotency key) — otherwise a transient failure after a non-idempotent tool call (e.g. "send email", "charge card") causes it to fire twice.
    • Default to retrying in model_middleware= for provider flakiness (rate limits, timeouts). Reach for middleware= retry only when you've confirmed the tools are safe to repeat.
  • Prefer railtracks.prebuilt.middleware.Retry(max_tries=..., approach=..., retry_on=...) over hand-rolling — it raises once attempts are exhausted rather than swallowing the failure, and lets you scope retry_on to specific exception types instead of catching everything.
  • Don't swallow exceptions silently inside a wrap_node/wrap_llm middleware (except Exception: return default) — that hides the failure from callers and from anything else in the chain. If you must return a fallback, do it deliberately (log it, tag it in the result) rather than as an unannounced side effect.

Read the full file on GitHub · 143 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. 2d ago Changed · +1 lines 981fa574572d
  2. 11d ago Changed b610bbbf131a
  3. 14d ago Changed · +17 lines 6b0ba8a1dede
  4. 22d ago Changed · +24 tokens per session d0d8cf143154
  5. 24d ago First seen · 125 lines · 21 tokens per session scan A 4e757de91056

Subscribe to this mod's changes

middleware is a skill published in the GitHub repository RailtownAI/railtracks (233 stars, last pushed today), licensed MIT. It adds 45 tokens to every session and 2,310 once invoked, about $0.0002 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-09-23.

Related

Other skills, from other repositories

first-reader

Beta readers for any draft, run by simulating how a real reader experiences it, moment by moment. A skim gate, a no-lookahead timed read producing an attention transcript with quit points, a recall test of what a reader remembers the next day, and a trust ledger of the implied author. Use when the user asks for a beta…

Shubhamsaboo/awesome-llm-apps · 174 tokens

mcp-apps-builder

MANDATORY for ALL MCP server work - mcp-use framework best practices and patterns. READ THIS FIRST before any MCP server work, including: Creating new MCP servers Modifying existing MCP servers (adding/updating tools, resources, prompts, widgets) Debugging MCP server issues or errors Reviewing MCP server code for…

Shubhamsaboo/awesome-llm-apps · 139 tokens

project-graveyard

Scans the developer's machine for dead side projects, autopsies each one from its git history (died at the payments wall, killed by a newer project, finished but never shipped), surfaces their personal death patterns, and picks the corpse most worth resurrecting — then helps ship it. Use when the user mentions…

Shubhamsaboo/awesome-llm-apps · 127 tokens

commit-archaeologist

Reconstructs why code exists from local git history, including the introducing commit, later changes, current authors, repeated companion files, and likely intent. Use when the user asks "why does this code exist", "who wrote this function and why", or to "explain the history of this function" before a rewrite…

Shubhamsaboo/awesome-llm-apps · 82 tokens

dependency-doctor

Checks requirements.txt, pyproject.toml, and package.json dependency manifests for surface-level direct-dependency footguns: standard-library shadowing pins, abandoned backports, unpinned dependencies, and obvious intra-manifest conflicts, plus opt-in PyPI yanked releases. Use when the user asks to check a manifest…

Shubhamsaboo/awesome-llm-apps · 115 tokens

scope-creep-detector

Analyzes git diffs against a stated intent to detect scope creep, unrelated files, broad pull requests, changes that grew beyond a fix, dependency additions, public API renames, config or CI edits, oversized hunks, and formatting-only files. Use when the user asks whether a change grew beyond the fix, a PR is too…

Shubhamsaboo/awesome-llm-apps · 98 tokens