View as Markdown

Foundation Models package

Any provider through Apple's Foundation Models framework

On iOS 27, Apple's Foundation Models framework lets an app plug in any LanguageModel. The AIProxy Foundation Models package is one: your app keeps Apple's LanguageModelSession, @Generable structured output, and tool calling, and the provider and model come from the AIProxy dashboard. Change them there and the app follows on its next request, with no release. The app ships no provider key and is authenticated with Apple App Attest.

import FoundationModels
import AIProxyFoundationModels

// The service URL from the dashboard: a service of type Apple Foundation Models router.
let model = AIProxyLanguageModel(serviceURL: URL(string: "https://api.aiproxy.com/<app>/<service>")!)
let session = LanguageModelSession(model: model)
let answer = try await session.respond(to: "Plan a 3 day trip to Kyoto.")

Structured output and tools work as with Apple's own model:

@Generable struct Trip {
    @Guide(description: "Destination city") var destination: String
    @Guide(description: "Three highlights") var highlights: [String]
}
let trip = try await session.respond(to: "Plan a 3 day trip to Kyoto.", generating: Trip.self)

let withTools = LanguageModelSession(model: model, tools: [WeatherTool()])
let reply = try await withTools.respond(to: "What is the weather in Paris right now?")

Setup

  1. In the dashboard, add an App Attest configuration to your app: your Apple team ID and the app's bundle ID. (Or POST /projects/:uuid/app_attest on the admin API.)
  2. Add a service of type Apple Foundation Models router, choosing the provider, the model, and the provider key. The key is encrypted with AWS KMS and only decrypted inside the proxy.
  3. Add the swift-aiproxy-foundation-models package to your app and pass the service URL shown on the service's page.
  4. Call try await model.authenticateIfNeeded() at launch so the one-time attestation happens before the first request. It takes about a second once per install.

Every AIProxy package takes exactly one value, the service URL, and derives the app and service from it. Services within the same AIProxy app share an attestation key, so each installation only needs to attest once.

The companion packages

PackageService typeUse
AIProxyFoundationModelsApple Foundation Models routerAIProxyLanguageModel for LanguageModelSession.
AIProxyRemoteConfigRemote configRemoteConfigClient(serviceURL:).fetch() returns the flags you set in the dashboard or with the admin API. Behaviour switches without a release.
AIProxyLogsOpenTelemetry log ingestAIProxyLogDestination(serviceURL:) plugs into LogAt so every #logAt line reaches the dashboard's Logs page and the admin API, tagged with the device's client ID.
AppAttestanyThe shared attestation client, for your own requests to an AIProxy service.
import AIProxyRemoteConfig
import AIProxyLogs
import LogAt

let flags = try await RemoteConfigClient(serviceURL: configURL).fetch()
let paywall = flags.string("paywall", default: "monthly_first")   // resolved for this device: variants, rollouts, overrides
let showBeta = flags.isEnabled("beta_composer")

let logs = try AIProxyLogDestination(serviceURL: logsURL)
LogAt.configure(thresholdLogLevel: .info, outputs: [.system, .custom(logs)])
#logAt(.info, "paywall shown", event: "paywall.result", attributes: ["variant": paywall, "outcome": "shown"])

Simulator

App Attest does not exist in the Simulator. The app's App Attest tab shows a bypass token; pass it through an environment variable in your Xcode scheme (AIPROXY_APP_ATTEST_BYPASS) and hand it to the packages' bypassToken: parameter. Never ship it.

How requests are authenticated

At first launch the package generates a key in the Secure Enclave, attests it with Apple, and registers it with AIProxy against your team and bundle ID. After that every request carries an assertion signed by that key over the request's method, path, body, a fresh nonce, the timestamp, and the key ID, so a request cannot be replayed or altered without the device. The exact headers and hash are documented under Build your own client. There is no per-request round trip to Apple or to AIProxy beyond the request itself. When a service has Require App Attest on, unattested requests are rejected with a message naming what is missing.