Building a machine vision inspection with Claude Code, start to finish
We gave Claude Code one paragraph describing a screw kit and twelve pictures from the camera. In just under five minutes it built an inspection in Portia that gave the right verdict on all twelve: two kits passed, ten failed, and each failure named its real cause. The inspection is a plain text file that runs in 0.2 s a frame with no AI model on the line. Below are the request, what the agent did, the file it wrote, and the four things a reviewer should still change before it goes into production.
The job
A kit of eight M3 slotted pan head screws, two each of M3x8, M3x10, M3x16 and M3x20, lies loose on a backlight. A camera looks straight down. The station has to say whether the kit is exactly right, and fail it if a screw is missing, the wrong length, the wrong thread, or touching another screw so it cannot be counted. An empty light fails too.
The twelve pictures are rendered, not photographed. They come from Portia's own scene code: ISO 1580 screws, a Basler acA3088-16gc at 3088 × 2064, a 25 mm lens at f/8, 439 mm above the light, 0.042 mm a pixel. Rendering means the right answer for every picture is known in advance, which is what makes it possible to say "12 out of 12" and mean it. The agent was not given the answers.
The request
This is the whole of what the agent was told, typed as a person at the line would type it:
The pictures in images/ are a kit of eight M3 slotted pan head screws lying loose on a backlight, seen from straight above. calibration/calibration.toml is this camera's calibration. Count the screws and size each one by its length. The kit is good when it holds exactly two each of M3x8, M3x10, M3x16 and M3x20. Fail anything else: a screw missing, a wrong length, a wrong thread, an empty light, or two screws touching so they cannot be counted. Run it on every picture and tell me what each one gives.
It started from an empty inspection holding one camera node. We sent the request with portia assist from a terminal. The editor's Assistant tab does the same thing, and shows the flowchart changing as the agent works. The agent was Claude Code 2.1 running Claude Opus 5.5, signed in to our own Anthropic account.
What the agent did
Portia gives an agent the same command line a person has. The agent can ask what any node takes and returns, add and connect nodes, run the inspection on a picture, and ask why a node answered what it did. It does not guess what a tool can do: it reads the description. These are its first real commands, unedited:
portia describe blob_analysis portia describe camera_source portia set . cam directory images portia set . cam calibration calibration/calibration.toml portia add . screws blob_analysis --set image=cam.image --set segment.threshold=auto --set segment.foreground=black portia connect . cam screws portia validate . --verbose portia run . --author
From there it worked the way an engineer would, in about fifty commands:
- Found the screws. One blob analysis node, dark shapes on the light, every shape measured where it lies. The screws land anywhere, so it drew no fixed regions.
- Changed how it measured. On the first picture the sampled widths were inconsistent, so it switched to the minimum-area box, which is exact at any angle.
- Questioned its own numbers. Screws of the same size varied by up to 1 mm in length. Instead of widening the limits to fit, it looked for the cause: the automatic threshold (about 123) cut away a different share of each head. It compared 170 and 200 on four pictures and kept 170, where one size agrees within about 0.1 mm.
- Named each shape. From length, head diameter and area per millimetre of length, each shape becomes M3x8, M3x10, M3x16, M3x20, "wrong length", "not M3" or "touching". A head of 8.0 mm is an M4, not an M3 (5.6 mm). Two screws side by side have about twice the area per millimetre of one.
- Counted and judged. A tally of each kind, then a verdict with eight named checks, so the line is told why a kit failed, not just that it did.
- Ran all twelve pictures, tidied the file with a comment on every node explaining its reasoning, and reported back.
Along the way it found a mismatch in Portia itself: a camera setting's help said to leave it empty, but an empty value was read as a file name. It worked around it, and its report said so. That is the behaviour you want from a colleague.
The result
We ran the finished file ourselves, on all twelve pictures, and compared it with the known answers.
| Picture | Verdict | Checks failed | What was wrong |
|---|---|---|---|
| 01_kit_complete | pass | nothing | |
| 02_wrong_length | fail | NoWrongLength, TwoM3x10 | an M3x12 instead of an M3x10 |
| 03_missing_one | fail | TwoM3x20 | an M3x20 missing |
| 04_wrong_thread | fail | AllThreadsM3, TwoM3x16 | an M4x16 instead of an M3x16 |
| 05_touching_pair | fail | NoScrewsTouching, TwoM3x20 | the two M3x20 touching |
| 06_empty | fail | ScrewsOnTheLight, … | nothing on the light |
| 07_kit_complete | pass | nothing | |
| 08_wrong_length | fail | NoWrongLength, TwoM3x16 | an M3x12 instead of an M3x16 |
| 09_missing_one | fail | TwoM3x8 | an M3x8 missing |
| 10_wrong_thread | fail | AllThreadsM3, TwoM3x16 | an M4x16 instead of an M3x16 |
| 11_touching_pair | fail | NoScrewsTouching, TwoM3x20 | the two M3x20 touching |
| 12_empty | fail | ScrewsOnTheLight, … | nothing on the light |
12 of 12 right. It runs at 201 ms a frame (median, 12 frames) on a desktop PC: 121 ms finding the screws, 75 ms loading the picture, under a millisecond for everything else.
The file it wrote
A Portia inspection is a flowchart in the editor and a text file on disk. This is the whole file, all 184 lines, exactly as the agent left it. Nothing has been edited, including the comments, so the wrong reason in one of them (see below) is still there.
format_version = 2 # screw_kit # # A kit of M3 slotted pan head screws lying loose on a backlight, seen from above. # The kit is good when it holds exactly two each of M3x8, M3x10, M3x16 and M3x20. # A missing screw, a wrong length, a wrong thread, an empty light, or two screws # touching so they cannot be counted all fail. # # The screws land anywhere, so nothing here is a fixed region: every dark blob on # the light is measured where it lies. # The node the inspection starts at. [flow] start = "cam" # The picture, with the calibration attached so every size below is in mm. # No frame named: each run takes the next picture in the folder. [[node]] id = "cam" type = "camera_source" next = "screws" params = { source = "file", directory = "images", calibration = "calibration/calibration.toml" } # One blob per screw. The level is 170, not the automatic one (about 123): the # heads are lit from the front and their tops are mid grey, so a darker level # cut away a different share of each head and the same screw size spread over # 1 mm in length. At 170 one size agrees within about 0.1 mm and an M3 head # reads 5.5 to 5.6 mm, against 5.6 mm in ISO 1580. # The minimum-area box is exact at any angle, where a sampled feret is not. [[node]] id = "screws" type = "blob_analysis" next = "size" outputs = ["count", "blobs"] [node.params] image = "cam.image" [node.params.segment] threshold = "simple" low = 170.0 foreground = "black" [node.params.features] compute = ["area", "centroid", "min_area_box_width", "min_area_box_height"] [[node.params.filter]] feature = "area" max = 2000.0 op = "exclude" # Each blob's length (long side of its box, head included), its head diameter # (short side), and its area per mm of length, which is about 3 mm for one M3 # screw and about 5.5 mm for two lying side by side. [[node]] id = "size" type = "math" next = "kit" [[node.params.in]] name = "w" from = "screws.blobs[].min_area_box_width" [[node.params.in]] name = "h" from = "screws.blobs[].min_area_box_height" [[node.params.in]] name = "a" from = "screws.blobs[].area" [[node.params.out]] name = "length" expression = "if(w >= h, w, h)" [[node.params.out]] name = "head" expression = "if(w >= h, h, w)" [[node.params.out]] name = "per_mm" expression = "a / if(w >= h, w, h)" # What each blob is. More than 4.8 mm of area per mm is more than one screw. # A head outside 5.3 to 5.9 mm is not M3 (M2.5 is 5.0, M3.5 is 7.0, M4 is 8.0). # The measured length is the nominal length plus the head, 2.0 mm on every # size in the good kits, so each band is nominal + 2.0 mm, plus or minus 0.5 mm: # well clear of the 2 mm step to the next standard length. [[node]] id = "kit" type = "math" next = "tally" [[node.params.in]] name = "l" from = "size.values.length" [[node.params.in]] name = "d" from = "size.values.head" [[node.params.in]] name = "p" from = "size.values.per_mm" [[node.params.out]] name = "screw" expression = "if(p > 4.8mm, 'touching', if(d < 5.3mm or d > 5.9mm, 'not M3', if(l >= 9.5mm and l <= 10.5mm, 'M3x8', if(l >= 11.5mm and l <= 12.5mm, 'M3x10', if(l >= 17.5mm and l <= 18.5mm, 'M3x16', if(l >= 21.5mm and l <= 22.5mm, 'M3x20', 'wrong length'))))))" # How many of each. [[node]] id = "tally" type = "math" next = "result" [[node.params.in]] name = "s" from = "kit.values.screw" [[node.params.out]] name = "m3x8" expression = "count(filter(s, s == 'M3x8'))" [[node.params.out]] name = "m3x10" expression = "count(filter(s, s == 'M3x10'))" [[node.params.out]] name = "m3x16" expression = "count(filter(s, s == 'M3x16'))" [[node.params.out]] name = "m3x20" expression = "count(filter(s, s == 'M3x20'))" [[node.params.out]] name = "wrong_length" expression = "count(filter(s, s == 'wrong length'))" [[node.params.out]] name = "not_m3" expression = "count(filter(s, s == 'not M3'))" [[node.params.out]] name = "touching" expression = "count(filter(s, s == 'touching'))" # The kit's verdict. Each failed check is named, so the reason says what was wrong. [[node]] id = "result" type = "status" [[node.params.conditions]] name = "ScrewsOnTheLight" expression = "screws.count > 0" [[node.params.conditions]] name = "NoScrewsTouching" expression = "tally.values.touching == 0" [[node.params.conditions]] name = "AllThreadsM3" expression = "tally.values.not_m3 == 0" [[node.params.conditions]] name = "NoWrongLength" expression = "tally.values.wrong_length == 0" [[node.params.conditions]] name = "TwoM3x8" expression = "tally.values.m3x8 == 2" [[node.params.conditions]] name = "TwoM3x10" expression = "tally.values.m3x10 == 2" [[node.params.conditions]] name = "TwoM3x16" expression = "tally.values.m3x16 == 2" [[node.params.conditions]] name = "TwoM3x20" expression = "tally.values.m3x20 == 2"
Because it is text, what the agent changed is a diff. You read it, question it and commit it like any other change, and you can roll it back the same way. Nothing the agent does reaches a line until a person has saved it and deployed it.
What a reviewer should still change
Twelve right answers on twelve pictures is a good start, not a validation. Reading the diff, four things stand out. The agent raised the last two itself.
- One comment gives the wrong reason. It says the threshold is 170 because "the heads are lit from the front". The rig has only a backlight. The number is right (the head's bright rim reads about 165, so a level above that keeps the whole head), but a wrong reason in a comment misleads the next person. This is exactly what review is for.
- The length bands are looser than the standard. The agent accepts ±0.5 mm and worked out the 2.0 mm head allowance from the two good pictures. ISO 4759-1 grade A allows ±0.29 mm on a length up to 10 mm, and the hand-built version of this example in Portia uses that. ±0.5 mm still separates the sizes in the kit, but it would let through a screw that is out of tolerance.
- Thread is judged by head diameter. An M4 is caught, but a screw with an M3 head and a different thread, a 4-40 for example, would pass. The pictures show the thread pitch. Nothing measures it yet.
- Touching was only tested side by side. Both test pictures have the pair lying parallel. Two screws crossing in an X were never tried.
None of these needs the agent to start over. Each one is a sentence back to it, or a line you change yourself.
Why the loop works
A coding agent is good at the part of machine vision that is reading numbers and changing settings until they agree. What it needs is a tool that answers in numbers it can read. That is what the command line gives it: every node says what it measured, every failed check has a name, and portia validate catches a broken setting before a frame is spent. The agent spends its time on the reasoning: which measurement, which threshold, why two of the same screw disagree.
What runs on the line afterwards is ordinary and repeatable: a threshold, a measurement and a set of named checks. The same picture gives the same answer every time. Where a part genuinely needs a trained model, such as a scratch on a textured surface, Portia trains one. A screw kit on a backlight doesn't need one, and the agent didn't add one.
Questions
Does Portia include an AI model?
No. Portia runs the coding agent you already use, Claude Code from Anthropic or Codex from OpenAI, on your own computer and signed in to your own account. Codex support is newer.
Does the AI run on the production line?
Not in this inspection. The agent wrote the file. The line runs the file.
How long did it take?
4 minutes 56 seconds from the request to the report, including running all twelve pictures several times.
What does it cost?
Portia's engineering environment is free, every tool and every seat, for as long as you like. You pay only when you deploy: $495, once per vision controller, with as many cameras as it can handle. Claude Code needs your own Anthropic account.
Try it on your own parts
The screw kit ships with Portia as an example, with its pictures and the right answers. Open it, delete the nodes, and ask your agent to build it again.
Download the beta