Key points
- A strict schema is the best defense against agent mistakes.
- Separate confirmed values from inferred ones.
- A small state machine prevents records from skipping approvals.
- Embroidery text should be stored raw and compared byte for byte.
When developers talk about making AI agents reliable, the conversation usually turns to prompts and model choice. For something as concrete as a group uniform order, the real leverage sits elsewhere: in the data model. A well-designed schema makes many mistakes impossible, and the rest easy to detect.
This is a developer-oriented walk through the records, rules and states behind an agent that collects sizes and names for caps and scrubs. It is a design sketch, not a product, and you should adapt it to your own stack and your facility's policies.
Start with the order line
The core unit is the order line, one per person per item. Resist the urge to store it as a free-text note. Give it explicit fields and types.
- person_id and display name, linked to a roster
- style, restricted to an enumerated approved list
- size, restricted to values on the supplier's chart
- embroidery_text, a raw string, nullable
- status, one of a small set of states
- confirmed_by_person_at, a timestamp, nullable
Using enumerations for style and size means the agent cannot invent a value. If the model proposes a size that is not in the list, validation fails and the agent must ask again. The constraint lives in code, so it holds even when the model has a bad day.
Separate what was said from what was inferred
Models are good at filling gaps, and that is a hazard. A line that says size medium because last year's record said medium is not the same as a line the person confirmed this week. Store both, in different fields.
A common pattern is to keep a suggested_value column and a confirmed_value column. The agent may populate suggestions freely, and the order sheet only reads confirmed values. A record with a suggestion but no confirmation is incomplete by definition.
This also makes debugging far easier. When something goes wrong, you can see whether the error came from the person, from the agent's inference or from a transcription step.
Model the lifecycle as a state machine
Each line moves through states, and only certain transitions are legal. A simple version looks like this: requested, drafted, confirmed by wearer, approved by manager, submitted, and closed. There are also side states for flagged and cancelled.
Encode the legal transitions in the application, not in the prompt. The agent can request a transition, but the code checks that the preconditions are met. A line cannot jump from drafted to submitted. A line with embroidery text cannot reach approved unless the wearer confirmation timestamp exists.
Every transition writes an event row with the actor, the time and the previous value. The result is an append-only history that supports audits and lets you reconstruct how a record ended up in its final state.
Treat embroidery text as a special type
Names and specialties deserve their own handling. Store the string exactly as received, normalizing nothing, and keep a hash of it at the moment of confirmation. When the order is assembled, recompute the hash and compare it to the stored one. If they differ, block the line.
Add supplier-specific constraints as validation rules: maximum characters per line, allowed character sets, number of lines, and thread color options. Where the rules are unclear, check the supplier's documentation rather than guessing. Anyone ordering scrub caps with name embroidery will want these limits encoded before the first batch goes out, because the stitched text cannot be corrected afterward.
One further subtlety is Unicode. Names with accents, apostrophes and hyphens may arrive in different encodings or with visually identical characters that are technically different. Normalize for comparison purposes only, and keep the original for output after the wearer has seen it.
Make idempotency a first-class concern
Agents retry. Networks fail. Without care, a retry can create a duplicate line or send two orders. Assign each line and each outbound order a stable identifier and make every write idempotent, so repeating a request has no extra effect.
For the final submission, add a lock. Once an order is marked submitted, further edits to its lines are refused, and any change request creates a new amendment record that goes through the full approval path. This prevents the quiet problem of a line changing after a manager approved the sheet.
Version everything the agent depends on
An agent's behavior depends on more than code: the prompt, the model, the approved style list and the supplier's size chart all change over time. Give each a version identifier and stamp it on every order line.
When a chart changes or a style is retired, you can then tell which records were created under the old rules. It also lets you roll back a bad prompt change quickly, and compare error rates before and after a new release.
Log for people, not just for machines
Plan for the person who will open the log after a problem. Record the agent's instructions version, the model identifier, the messages exchanged with each person and any tool calls with their results. Redact what you do not need, and set a retention period.
Remember that the roster contains personal information. Limit the fields the agent can read to those needed for ordering, and apply the access controls your organization requires. Check with your privacy or compliance team about what applies in your setting.
A short test plan
- Attempt to write an invalid size and confirm the write is rejected.
- Try to skip a state and verify the transition fails.
- Submit the same order twice and confirm only one is created.
- Alter embroidery text after confirmation and confirm the hash check blocks it.
- Replay a log and verify the final state matches.
None of this is glamorous, and that is the point. An agent running on a strict schema can be wrong in conversation and still be safe in the system, because the data layer refuses to accept what should not be accepted. Keep the schema simple at first, add constraints as mistakes appear and never let the model be the only safeguard.
Topics: data schema, ai agents, validation, developer guide
Updated
