Skip to content

Repository files navigation

AgentRunKit

AgentRunKit

CISwift 6.1PlatformsOn-Device MLX + Foundation ModelsSPMLicenseDocumentation

A Swift 6 SDK for building LLM-powered agents with type-safe tool calling.

Zero-dependency core · Full Sendable · Async/await · Cloud + Local · MCP


Quick Start

import AgentRunKit
letclient=OpenAIClient.openAI(apiKey:"sk-...", model:"gpt-5.4")letweatherTool=tryTool<WeatherParams,String,EmptyContext>(
name:"get_weather",
description:"Get the current weather"){ params, _ in"72°F and sunny in \(params.city)"}letagent=Agent(client: client, tools:[weatherTool])letresult=tryawait agent.run(userMessage:"What's the weather in SF?", context:EmptyContext())iflet content = result.content {print(content)}

result.content is optional. A completed run returns the content of whatever ended it — the built-in finish tool, or a completion tool you supply — while structural terminal reasons such as max iterations or token budget exhaustion surface through result.finishReason with no final content.

To end the loop with your own typed tool instead of the built-in one, pass it as completionTool:. It replaces finish in the definitions sent to the model, executes through the normal tool path, and terminates the run only when it succeeds:

letagent=Agent(client: client, tools:[searchTool], completionTool: publishTool)

Documentation

Full documentation including guides and API reference is available on Swift Package Index.


Runnable Example

Examples/AgentCode is an interactive terminal coding agent built with AgentRunKit. It demonstrates the full agent loop in a local workspace: streaming events, type-safe tools, approval-gated edits and command execution, bounded file access, transcript export, and deterministic offline mode.

cd Examples/AgentCode
swift run agent-code

By default it opens a bundled broken Swift package so you can ask it to fix failing tests. Set OPENAI_API_KEY for a live OpenAI-compatible provider, or run without a key to exercise the CLI with the offline test client.


Installation

Add to your Package.swift:

dependencies:[.package(url:"https://github.com/Tom-Ryder/AgentRunKit.git", from:"5.5.0")]
.target(name:"YourApp", dependencies:["AgentRunKit"])

For on-device inference, additional targets are available:

  • AgentRunKitMLX for MLX on Apple Silicon (links mlx-swift-lm)
  • AgentRunKitFoundationModels for Apple Foundation Models (iOS 26+ and macOS 26+, no external dependencies)

Features

  • Agent loop with configurable iteration limits and token budgets
  • Streaming with AsyncThrowingStream and @Observable SwiftUI wrapper
  • Type-safe tools with compile-time JSON schema validation
  • Sub-agent composition with depth control and streaming propagation
  • Context management: automatic compaction, pruning, token budgets
  • Checkpoint and resume: per-iteration snapshots, file or in-memory backends
  • Tool approval: human-in-the-loop policies with session allowlists
  • Structured output with JSON schema constraints
  • Multimodal input: images, audio, video, PDF
  • Text-to-speech with concurrent chunking and MP3 concatenation
  • MCP client: stdio transport, tool discovery, JSON-RPC
  • Extended thinking / reasoning model support

Providers

ProviderDescription
OpenAIClientOpenAI and compatible APIs (OpenRouter with reasoning replay, Groq, Together, Ollama)
AnthropicClientAnthropic Messages API with same-substrate continuity replay
GeminiClientGoogle Gemini API
VertexAnthropicClientAnthropic models on Google Vertex AI with same-substrate continuity replay
VertexGoogleClientGoogle models on Vertex AI
ResponsesAPIClientOpenAI Responses API with same-substrate continuity replay and OpenRouter factory
FoundationModelsClientApple on-device (macOS 26+ / iOS 26+)
MLXClientOn-device via MLX on Apple Silicon

Requirements

PlatformVersion
iOS18.0+
macOS15.0+
Swift6.1+
Xcode26+ for local development and CI

License

MIT License. See LICENSE for details.

About

Swift 6 agent SDK: type-safe tools, streaming, cloud + on-device inference via MLX on Apple Silicon

Topics

Resources

Contributing

Stars

29 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages