Start with the contract, not the model
In a legacy integration, the main risk rarely lies only in the model’s textual response. It appears when an ambiguous output crosses an operational boundary and reaches a system that expects very specific codes, dates, identifiers or states. The first decision should therefore define what the AI may produce, in which format and with which permitted consequences.
A data contract turns that boundary into a verifiable interface. It should declare required fields, types, accepted values, size limits, absence rules and the meaning of each state. It should also separate suggestion from execution. A reimbursement recommendation, for example, cannot be treated as authorization to issue the reimbursement.
For a well-bounded task, a workflow with fixed steps is usually more predictable than an agent that freely decides which tools to use. The practical recommendation is to begin with a simple call or deterministic flow and add autonomy only when testing shows a concrete need. The distinction between predictable workflows and flexible agents supports this choice. [sourceId 1]
- Define the interface between the AI and the legacy system as if it were a public API, even when both belong to the same company.
- Keep the AI away from irreversible operations until there is an explicit authorization and validation step.
Sources and references: [1]
Make the output pass through a validation layer
The response received from the model should be treated as untrusted input until it passes executable checks. First, validate the structure, including well-formed JSON, known fields and correct types. Then validate the domain by checking whether the identifier exists, whether the date is plausible and whether the requested state is allowed for that record. Finally, validate the business rule in the legacy system itself, rather than delegating it to the prompt.
This separation reduces a common problem: confusing a text instruction with a business value. A comment included in a service note, a field copied from an email or a retrieved document may try to influence the model’s decision. External content should be treated as data, not as an instruction with higher priority. Structured outputs, enums and fields with limited values help prevent free text from traveling through the entire chain.
The validation layer also needs to produce readable reasons for rejection. “Missing field,” “prohibited state transition” and “insufficient permission” are much more useful diagnoses than “the model failed.” This information feeds metrics, regression tests and the fallback path. Structuring data between steps reduces free channels for injection, but does not eliminate every risk, so authorization must remain in the code that controls the operation. [sourceId 2]
- Use versioned schemas and reject unknown fields when they could change the process behavior.
- Revalidate critical data after any transformation, enrichment or tool call.
- Record the technical reason for rejection without storing more personal data than the diagnosis requires.
Sources and references: [2]
Test the contract under normal and adverse conditions
Traditional integration tests are not enough when one step produces variable language. The evaluation set should test the business task, tool selection, submitted arguments and behavior when inputs are incomplete or manipulated. The goal is not to prove that the model will always respond in the same way, but to verify that acceptable variations remain within the contract and dangerous variations are blocked.
Build cases with valid records, missing fields, old formats, duplicates, unexpected characters, contradictory requests and partially unavailable services. For each case, define the expected result at the process level: accept, request information, route to a known workflow or return to the previous behavior. Compare integration versions with domain-specific tests instead of relying on a general impression of quality.
Evaluations should track changes to the prompt, schema, adapter and permissions. It is useful to measure structural compliance, argument accuracy, justifiable rejection rate and flow completion without improper action. Continuous evaluations and datasets that grow with new cases are recommended for handling variability in model-based systems. [sourceId 3]
- Separate extraction, validation and execution tests to identify exactly where a case was diverted.
- Include inputs that look like instructions but are only content retrieved from external sources.
- Define an operational limit that stops repeated attempts, reprocessing loops or circular handoffs.
Sources and references: [3]
Design the fallback before activating the AI
A fallback path is not simply turning off the model. It is an operational route defined for situations in which the response cannot be validated, the service is unavailable or the business rule cannot be applied safely. Depending on the task, the fallback may preserve the original input, use the existing deterministic rule, place the item in an exception queue or request missing data. The choice should be made by failure type, not by a generic message.
Also separate technical failure from low functional confidence. A response with invalid JSON requires a different action from a valid response that proposes a prohibited transition. The adapter should record correlation, contract version, validation result and action taken, without granting the model broad access to logs, databases or commands. Reading and writing tools should have separate scopes, and operations with impact should require explicit confirmation in the appropriate flow.
To avoid partial effects, prefer idempotency, transactions or a preparation step before confirmation. If the legacy system does not offer transactions, record an intent with a pending state and allow reconciliation through a controlled routine. The recommendation to maintain tool approvals and limit what untrusted data can direct is especially relevant in integrations with external systems. [sourceId 2]
- Classify each error as data rejection, unavailability, permission, business rule or timeout.
- Ensure that the fallback does not automatically repeat an action that could duplicate a charge, record or update.
- Give the operator enough context to continue the flow without exposing credentials or unrestricted access.
Sources and references: [2]
Hypothetical example of gradual implementation
Consider a company that receives requests for record changes in a legacy system. The first AI component extracts the name, account identifier and type of change from the message. It does not access the database directly. It produces only an object with defined fields, a completeness level and an indication that information is missing. An adapter checks the schema, queries a limited read-only API and compares the change with the existing rules.
If the identifier does not exist, the process returns a correction request. If the change requires a transition that the legacy system does not accept, the request leaves the automated flow and follows the conventional route. If the API is unavailable, the system preserves the original message and records a pending intent without making a blind attempt. Only after all validations have passed is an allowed operation prepared, while confirmation remains separate from the interpretation made by the AI.
Deployment can begin in observation mode, in which outputs are evaluated against the contract without changing records. Next, a small class of requests can use the adapter with fallback enabled. Progress should depend on repeatable tests, understandable failure signals and a real ability to stop the automated path. The simplest architecture that satisfies these requirements is preferable to an agent with broad access that is difficult to debug. [sourceId 1]
- Choose a single reversible operation for the first pilot, using synthetic or isolated data whenever possible.
- Version the contract together with the adapter and maintain explicit compatibility during the transition.
- Treat each permission as part of the interface design, not as a later configuration.
Sources and references: [1]
Put it into practice
- Design the input and output contract, including types, required fields, enums, versions and rejection conditions.
- Implement structural validation, domain validation and business rule validation outside the model.
- Define minimum permissions for each tool, separating reading, preparation and execution of operations.
- Create a fallback route for each failure class, with diagnostic records and protection against duplication.
- Build evaluations with normal cases, boundary cases, manipulative content and unavailability before expanding scope.
Common questions
Does a data contract replace validation of the legacy system’s rules?
No. The contract checks format and permitted values at the interface, but business rules still belong to the system responsible for the operation and must be applied again before execution.
When does it make sense to use an agent instead of a fixed workflow?
An agent may make sense when the number of steps varies in ways that are difficult to predict and there is an objective way to verify each result. Even then, tools, limits, approvals and fallback paths must be defined.
What should happen when the AI output is valid but the action is prohibited?
The process should reject the action, record the reason and follow a predefined route, such as requesting correction, using the conventional workflow or keeping a pending intent without changing the legacy system.
Sources and references
- Anthropic: Building effective agents ↗Accessed on October 2, 2026
- OpenAI: Safety in building agents ↗Accessed on October 2, 2026
- OpenAI: Evaluation best practices ↗Accessed on October 2, 2026



