Swift agents
Agents contracts and source references for Swift.
AgentConfig takes providerRef, optional system prompt, limits, generation options, and optional name. ForgeAgent.run(input:) is async throws and returns AgentOutput. Read text, usage, steps, and finishReason; use try await and preserve task cancellation. This differs from the TypeScript initialization API.
A bounded run
This complete function accepts a model already configured by the caller. No tools are registered. Supply a model supported by your application and handle the returned error or exception at the caller. This example does not provision provider credentials, identity or deployment.
import ForgeAgent
import ForgeCore
import ForgeTool
func runAgent(
model: any LanguageModel,
providerRef: String,
prompt: String
) async throws -> AgentOutput {
let agent = ToolLoopAgent(
model: model,
config: AgentConfig(providerRef: providerRef, maxSteps: 3),
approvalHandler: DenyAllHandler()
)
try agent.start()
defer { try? agent.stop() }
return try await agent.run(input: prompt)
}start() prepares the Swift instance. stop() in the deferred cleanup is best effort if the run already failed.
Swift reference
Each reference records the source path, hash and declaration inventory for this language. Start at the import boundary before using a symbol found in a deeper implementation file.
Lifecycle and failures
Choose finite iteration and token budgets, handle generation and execution failures, and release resources on termination. Inspect the returned usage and completion state before recording a successful run. The SDK health/lifecycle object describes local runtime state; it is not a deployment health check.