Make asynchronous Compute calls in Swift
How to read a Compute job from Swift with async and await, handle cancellation and errors, and read encrypted result details.
Use LithiClient with async throws for one typed job read. Your existing agent keeps its model,
goal, project context and final decision; this client exposes the current API operations. Treat task
cancellation separately from a server-side job cancellation.
The example below injects an LithiTransport, so it stays local and uses synthetic response data. A client built with your credential and the default URLSessionTransport sends requests to your account.
1. Construct LithiClient
Import the LithiClient module and initialize the class with a baseURL and an async credential
provider. The package declares Swift tools 5.9 and macOS 12 or later. Its default transport is
URLSessionTransport; inject LithiTransport when you need an offline test.
import Foundation
import LithiClient
struct FakeTransport: LithiTransport {
func send(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) {
let body = #"{"ok":true,"data":{"job_id":"550e8400-e29b-41d4-a716-446655440000","state":"queued","state_version":1,"digest":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","p_next_action":"none"}}"#
let response = HTTPURLResponse(
url: request.url!, statusCode: 200, httpVersion: nil,
headerFields: ["Content-Type": "application/json"]
)!
return (Data(body.utf8), response)
}
}
let client = try LithiClient(
baseURL: URL(string: "https://api.example.test/")!,
credentials: { "lthi_cmp_" + "0123456789abcdef" + "." + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ" },
transport: FakeTransport(),
retries: 0
)
let job = try await client.getJob("550e8400-e29b-41d4-a716-446655440000")
print(job.state, job.state_version)The URL, key, response, and job ID are synthetic. FakeTransport makes no network request.
Credential providers and operation methods use Swift concurrency, so calls are async throws.
2. Make an asynchronous read
The client exposes createQuote, createJob, getJob, cancelJob, listJobEvents,
getJobResult, createBatch, getBatch, listBatchResults, prepareArtifact,
completeArtifact, listCapabilities, listUsage, getReceipt, listAgentEvents,
compatibilityOpenaiResponses, compatibilityOpenaiChatCompletions,
compatibilityOpenaiEmbeddings, and compatibilityAwsBatch. These methods are flat on
LithiClient.
POST methods require a key: argument. cancelJob accepts an optional JobRequest body plus
key:. Paged methods accept limit: and optional cursor: and default to 50. The constructor
defaults to a 30-second timeout, two retries, and a 4 MiB response limit. Request bodies are
limited to 65,536 bytes; the response limit may be configured up to 16 MiB.
3. Handle cancellation and results
LithiError distinguishes contractUnavailable, invalidRequest, credentialUnavailable,
typed HTTP errors, responseInvalid, network, timeout, and aborted. A canceled Swift task
maps to aborted and ends the call. It doesn't cancel an admitted Compute job. Call cancelJob
when you need the separate server operation.
getJob returns a typed state projection. getJobResult returns encrypted artifact metadata and
digests, not decrypted rows or artifact bytes. Retrieve authorized bytes through the documented
flow and verify their digest and media type.
If a POST outcome is uncertain, preserve its body and key, read the existing job, and reuse the same intent. Don't generate a new key to decide whether the first request arrived.
Check the compatibility matrix, then use the Work API reference for wire schemas.
The Swift wire client has no monthly-report operation. Reading that report needs separate permission and an approved reporting API or file path; it remains outside the five MCP work tools.
Swift supplies the wire client
The supplied Swift source provides the asynchronous Compute wire client, typed contracts, transport injection, retries and cancellation handling. It does not include the TypeScript Kit's local record preparation, pseudonymization, compile/seal workflow or complete result-delivery steps. Check the client matrix for the boundary; the client does not grant transfer or execution authority.
The LithiClient package name and module identity do not imply a public package-registry install.
Use the exact local source and release supplied to your project. For a concrete support-ticket
flow, read What leaves your machine, then verify result
files before choosing a delivery path. The Kit
helper reference compares the local helper coverage by language.
See Kit contents for the supplied modules and workflow guides.