Swift AI agents — Make AI work a program you can reason about.
A model call becomes useful when its result fits the next piece of work. SwiftAgent composes AI processing into the application’s own typed execution flow.
Give each part of the work a boundary.
Summarization, extraction, transformation and validation have different responsibilities. Hiding them in one prompt makes intermediate behavior difficult to inspect. Step<Input, Output> describes each transformation; its output connects to the next input, and the application receives a typed result.
This boundary is executable, not just a type label. StepBuilder.buildBlock requires S1.Output == S2.Input; incompatible steps cannot form the chain. A changed model output therefore cannot silently cross an incompatible downstream interface. Chain.run executes its stages with try await and returns an intermediate failure to its caller.
An agent is a program.
PHILOSOPHY.md makes types, tools, runtime policy, observation and cancellation visible rather than hiding behavior behind magic. Types narrow interfaces; they do not establish factual truth. Instructions guide a model, while runtime code must enforce permissions. These are different responsibilities.
String-based connections are flexible, but move responsibility for checking the next stage’s expected structure into runtime code. Typed Steps give that connection condition to the compiler. Sharing a type still does not establish shared meaning: acceptable amounts, permissions and valid domain results need explicit Gates or other application checks.
Put validation inside the execution flow.
A composed Step delegates run to its body, and Pipeline executes its content. Gate passes a value with .pass or throws GateError for .block. This separates input checks, generation and output checks into code. The inspected tests describe both typed transformations and blocked execution.
- ApplicationInput
- SwiftAgentStep<Input, Output>
- SwiftAgentGenerate / Gate
- ApplicationOutput
public func run(_ input: Input) async throws -> Output {
switch try await handler(input) {
case .pass(let output): return output
case .block(let reason):
throw GateError.blocked(reason: reason)
}
}Keep an empty input out of the next stage.
In GatePipelineTests, pipelineGateBlocks checks an empty String and returns .block. A Transform follows it, but Gate.run throws GateError, so the chain does not reach that transformation. Success passes a value; rejection returns a failure. The decision lives in execution control rather than in a model’s written response.
pipelineWithGates[HELLO]pipelineGateBlocksGateError
A well-typed result still needs judgment.
The public README requires Swift 6.4+, iOS 26+ and macOS 26+. Model and tool availability depend on the selected runtime. The Gate shown here validates values; it is not a claim that the whole tool runtime is secure. Domain validity remains an application responsibility.
This matters when AI results lead to storage, messages or operations. “A result exists” and “the application accepts it” can be separate stages, with rejection returned to the caller. This describes Gate’s value validation, not the security of all tool execution. Each stage’s authority still belongs in application and runtime-policy contracts.
Read the evidence
Source reading date:
An explanation based on reading public design, implementation and test bodies. It is not a report of executing upstream tests or measuring device performance.