algorithmic-patterns

A reference for choosing efficient algorithms and data structures. It explains how the amount of work grows as the input gets larger.

In plain words
What is it for?
Use it when changing loops, lookups, collections, or imports outside the transport and cache code, especially when reviewing pull requests.
Why use it?
It helps reveal slow repeated searches, nested loops, repeated sorting, and full-directory scans before they become performance problems.

Agent

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.

agentmods
npx agentmods add agents/microsoft/apm/algorithmic-patterns
Clone the repo
git clone --depth 1 https://github.com/microsoft/apm
Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,264 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
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 $0.00000 $0.01264
Opus 5 $0.00000 $0.00632
Sonnet 5 $0.00000 $0.00253
Haiku 4.5 $0.00000 $0.00126

Measured yesterday against content hash 278321484288, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

algorithmic-patterns 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 yesterday.

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.

.github/agents/algorithmic-patterns.agent.md · 153 lines

How it starts

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

Algorithmic Performance Patterns

Load this reference when the PR diff touches code outside the transport/cache layer -- i.e. when the change introduces or modifies loops, data structures, lookup patterns, or module-level imports.

Big O Quick Reference

Pattern Complexity Red Flag
Dict/set lookup O(1) Fine
List .append O(1) amortised Fine
x in list O(n) Use a set if called in a loop
Nested loops over same collection O(n^2) Extract an index dict first
Sort inside a loop O(n^2 log n) Sort once outside the loop
any(pred(x) for x in coll) in a loop O(n*m) Build a set/dict pre-loop
Unconditional full-dir scan on every write O(n) per write = O(n^2) total Track running total; scan only when needed
Linear search for identity match O(n) per lookup Build {identity: index} once

Anti-Patterns to Flag

1. Missing Index on Repeated Lookup

# BAD: O(n) per call, called m times = O(n*m)
def has_item(collection, key):
    return any(item.key == key for item in collection)

# GOOD: O(1) per call after O(n) setup
_index = {item.key for item in collection}
def has_item(key):
    return key in _index

Flag when: a function does linear scan AND is called from within a loop or from a method called repeatedly during resolution/install.

2. Unconditional Expensive Operation

# BAD: scans entire cache dir on every store()
def store(self, url, body):
    self._write(url, body)
    self._enforce_size_cap()  # full scandir every time

# GOOD: fast-path skip when clearly under budget
def store(self, url, body):
    self._write(url, body)
    self._tracked_size += len(body)
    if self._tracked_size > MAX_SIZE:
        self._enforce_size_cap()  # scan only when needed

Flag when: an expensive operation (directory walk, sort, full re-computation) runs unconditionally on every call to a high-frequency method.

3. Triple-Pass Where Single-Pass Suffices

Read the full file on GitHub · 153 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. yesterday First seen · 153 lines · 0 tokens per session scan A 278321484288

Subscribe to this mod's changes

algorithmic-patterns is an agent published in the GitHub repository microsoft/apm (3,668 stars, last pushed yesterday), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 1,264 tokens. 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.