Skip to content
Brad Johnson, home
All words · 7 min read

An Image Library That Agents Can Use

Tags:#ai#agents#netlify

Netlidam is a digital asset manager for images that also serves them, the way Cloudinary does. A team uploads photos, agents tag, caption and box them, people review what the agents weren’t sure of, and sites embed URLs that resize and crop on the fly. The other users are AI agents, which get the same library through an MCP server.

I wanted to know whether this could run on Netlify alone, so it does. The first commit was on November 25. Six spikes ran that day, and the last of the main features was in by the 27th.

The stack

Concern Choice
App Vite, React, TanStack Router and Query, under /app/*
API and MCP Hono on Netlify Server, running Effect programs
Data and search Netlify Database: Postgres 18 with pgvector and row-level security
Originals Netlify Blobs, keyed by content hash
Ingest Async Workloads, one step per stage
Delivery An edge function in front of the Image CDN
Auth and orgs WorkOS AuthKit, for the app and for MCP

Netlify Server is in Early Access, and its limits shaped the code. Response headers have to start within 20 seconds, cold start included. An instance stops after 5 idle minutes and is replaced after 8 hours, so anything durable goes in Postgres or Blobs and anything long goes to a workload. Outbound connections close after 60 idle seconds, so the database pool’s idle timeout is shorter than that.

Every route runs an Effect program through one ManagedRuntime built when the instance starts. Typed errors map to status codes, and the shutdown hook waits for in-flight requests before disposing it.

Uploads

An upload is authorized before its body is read. The body streams in with a 50 MB cap, and the format comes from the bytes, never the content type. JPEG, PNG, WebP and GIF are stored as they are. AVIF and TIFF also get a master the Image CDN can read, made with Sharp.

Blobs are keyed {org}/sha256/{hash} and written once, before any database row, so a failed insert leaves an orphan blob and never a row pointing at nothing. Duplicates are caught within an org, never across orgs, because a global check would tell one customer whether another holds a file. Deploy previews get their own database branch but share Blobs with production, so request handlers never delete a blob.

Ingest

Each upload queues a workload:

  1. prepare makes the analysis input with Sharp: EXIF rotation applied to the pixels, the long side fit to 1024 px, small images padded with the padding recorded, baseline JPEG.
  2. analyze and caption run in parallel. One request to Jev, TypeSafe’s model, asks 16 questions about the photo: what kind of image it is, whether it shows a real person, text, a logo, a watermark or personal information, and boxes around the subject, the most prominent face and all the people. Claude Haiku, through Netlify’s AI Gateway, writes a caption and alt text.
  3. check-caption sends the caption and alt text back to Jev with the photo, asking whether they’re accurate and free of personal information. The lower of the two answers becomes the caption’s confidence.
  4. save writes one row per field with its source, model and confidence, and maps boxes back to the photo’s frame.

Before building any of this, I ran Jev over 24 test photos to see whether its confidence meant anything. It did. Tight, correct boxes scored 0.8 to 0.92 and wrong ones mostly under 0.35, at about $0.0001 per image. The spike also found what to ask for. The face box finds a “face” in a photo with no face in it, so a separate yes/no question about whether there’s a face gates it. A face on a Times Square billboard counted as a face, so there’s a question about whether it’s a real person in the scene. Group shots needed a box around everyone.

Confidence and review

Every machine-made label, caption and crop carries its source and confidence, and that decides what happens to it. A caption under the org’s threshold (0.75 by default) goes to the review queue. So does a yes/no label between 0.35 and 0.65, a sensitive label like a watermark or personal information that says yes without being sure, and an image whose boxes are too weak to crop from. The queue is computed from the metadata, not stored.

A reviewer’s decision is a new row with source human, so the agent’s answer stays in history and the newest value wins. Only text a person confirmed, or an agent was sure of, gets indexed for search or returned as alt text, so a doubtful caption can’t make an image match a query or end up in someone’s <img> tag.

Delivery URLs

The URL grammar follows Cloudinary’s underscore style:

/img/{org}/hero/blue-mug                           preset
/img/{org}/w_800,ar_16:9,c_smart/blue-mug          transforms
/img/{org}/w_400,dpr_2/v7/blue-mug.avif            pinned version
/img/{org}/s--Xa9k2Qe1Fh3pLm0w--/w_1234/blue-mug   signed
/img/{org}/lqip/blue-mug                           placeholder
/img/{org}/blue-mug.json                           metadata

Every transform costs money, so the unsigned set is small: presets, widths snapped to a fixed list, a fixed set of ratios, and quality in steps of ten. Anything else needs a signature, the first 16 characters of an HMAC over the transforms and id under a per-org key. The edge checks it with Web Crypto before touching storage, and nothing goes past 6000 px on a side.

The edge function is kept small because it runs on every request, cache hits included. It parses the path, reads one alias from Blobs (5 to 10 ms), and rewrites to the Image CDN with the source hash in the URL. Replacing an image needs no purge, since the new hash is a new cache key the moment the alias changes. Unversioned URLs get a 60-second browser cache, and versioned ones keep the CDN’s immutable year.

c_smart uses the boxes from ingest. A manual crop for that ratio wins. Otherwise a trusted real face stays in frame with headroom, a people box clearly bigger than the subject keeps the group, a confident subject stays inside, and anything less certain gets a center crop. The rectangle goes into the source URL, where Netlify Server cuts it with Sharp and the CDN resizes it, so a changed crop is a new cache key too.

The .json URL is the one written for agents and front-end code. It answers at the edge with the size, a srcset no wider than the source, smart-crop URLs and srcsets for 1:1, 4:5, 16:9 and 3:2, a 24 px placeholder of about 120 bytes, and alt text only when it’s trusted.

Images are draft, scheduled or published, and the alias carries that state, so the edge returns 404 for an unreleased image without asking the database and a scheduled release needs no job. The app shows drafts through a signed view cookie, and a 7-day link shares one draft with someone outside.

Each image has one search row holding a weighted tsvector and a 512-dimension embedding of what it shows. A query runs full text and vector search and fuses them by reciprocal rank. Then Jev looks at each of the top 20 images with the query and answers whether someone searching for it would want that photo. Confirmed matches move up, clear misses move to the end, and nothing is dropped. The checks run ten at a time with a 2.5-second limit each, so a slow answer leaves that image where fusion put it and search never fails because of Jev.

On the test set, the right images scored 0.78 to 0.97 and the rest mostly under 0.2. “City street at night” had no real match, and nothing scored above 0.15. The rerank adds about half a second to a second to a search.

Agents over MCP

/mcp is a stateless Streamable HTTP server. AuthKit is the authorization server, with dynamic client registration turned on, so adding Netlidam to Claude, ChatGPT or Cursor is pasting the URL and signing in. Netlidam serves the protected-resource metadata, checks that each token was issued for its own /mcp URL and carries an org, and looks up the member’s role.

Tools run as that person, through the same services and permission rules as the app. An agent can search, list recent images, get an image’s caption, alt text, srcset and an <img> tag, get a delivery URL (signed automatically when it needs to be), edit captions, read the review queue and add images to collections. Drafts are marked, and agents are told not to place them.

Orgs and teams

A WorkOS organization is a Netlidam org, and org roles and permissions travel in the access token. The library is shared across the org, and teams decide who uploads, edits, deletes and publishes. Those rules are pure functions in one file, which makes them easy to test.

Every table carries org_id with row-level security on it. The database connection role bypasses RLS, so tenant requests open a transaction that switches to a role without that bypass and sets the org in a transaction-local setting, both in one round trip. A request with no org setting matches no rows.

Netlidam is a Wheel of Time name, like everything else I build. It was called Tanchico until today.