Skip to main content
coreydaley.dev

Can a tool teach Apple's on-device model tic-tac-toe?

In the games post, Apple’s on-device model lost most of its tic-tac-toe games the same way: it named a square that was already taken, with the list of legal moves right there in the prompt. In the follow-up it did well at pulling data out of text, the job it’s built for.

So, a fair question: if the problem is keeping track of the board, can I give it a way to look? The Foundation Models framework, which I first tried from Swift, lets an app hand the model tools, Swift functions it can call while it answers, and can constrain an answer to a set of values. I tried both.

The setup

A small Swift program on macOS 27.2 (M2 Pro), playing through the same referee as the games post: the same prompt, with the board and a line of legal moves in order; the same opponent, a perfect minimax player that breaks ties at random; and the same 20 games and seeds, half as X and half as O. Each move was a fresh session with default settings. Four ways:

  • No tool, as a baseline.
  • Guided generation. The answer is a schema whose only allowed values are the empty squares, so an illegal move can’t be generated.
  • A checkMove tool. The model passes a square; the tool says whether it’s empty. The instructions say to check before answering.
  • A freeSquares tool. It returns the list of empty squares. The instructions say to call it before choosing.

The checkMove tool is a Swift struct. The model fills in Arguments, and whatever call returns goes back into the conversation:

struct CheckMove: Tool {
    let board: [Character]
    let name = "checkMove"
    let description = "Checks whether a tic-tac-toe square (1-9) is empty and can be played."
    @Generable struct Arguments {
        @Guide(description: "The square number, 1 to 9")
        var square: Int
    }
    func call(arguments: Arguments) async throws -> String {
        let s = arguments.square
        guard (1...9).contains(s) else { return "Square \(s) doesn't exist. Squares are 1 to 9." }
        let c = board[s - 1]
        return c == " " ? "Square \(s) is empty. You can play it."
                        : "Square \(s) is taken by \(c). Choose another square."
    }
}

let session = LanguageModelSession(
    tools: [CheckMove(board: board)],
    instructions: "Before you answer, call the checkMove tool with the square you want, and pick another if it's taken.")

Guided generation needs the allowed squares at run time, so it uses a dynamic schema instead of a @Generable type:

let legal = (0..<9).filter { board[$0] == " " }.map { String($0 + 1) }
let move = DynamicGenerationSchema(name: "Move", anyOf: legal)
let root = DynamicGenerationSchema(name: "Answer", properties: [
    .init(name: "square", schema: move)])
let schema = try GenerationSchema(root: root, dependencies: [])
let answer = try await LanguageModelSession()
    .respond(to: prompt, schema: schema).content
let square = try answer.value(String.self, forProperty: "square")

The results

ModeDrawsLossesIllegal or failedTypical time per move
No tool14150.3 s
Guided generation21800.4 s
checkMove tool01822.1 s
freeSquares tool05153.8 s when it worked; 3–5 minutes when it failed

The baseline is close to what the same model did through fm in the games post (one draw, five losses, fourteen illegal moves), so the Swift program sees the same behavior.

Guided generation fixed the rule-breaking completely. No illegal moves in 20 games, for about a tenth of a second more per move.

checkMove fixed it too, but not the way I’d hoped. I expected the model to propose a square, check it and change its mind when it was taken. It never did: across 62 moves and 266 checks, it didn’t ask about a taken square once. Its first check was always the lowest-numbered empty square, the first entry on the prompt’s “Legal moves” line, and it played that square in 50 of 60 moves. As X it opened in the corner, square 1, in nine of ten games, where without a tool it opened in the center. With no tool it played the first legal square in 1 move of 41; with guided generation, 7 of 61. The tool, or the instruction to use it, turned it into a player that takes the first square on the list. That’s always legal, and it loses.

It was also slow: a median of four tool calls per move and up to 20, often repeating the same check. Its two failures weren’t illegal moves: each request ran past the model’s 8,192-token context and failed (“Content contains 8194 tokens, which exceeds the maximum allowed context size of 8192”).

freeSquares made things worse. Half its moves, 15 of 30, failed the same way after three to five minutes, with errors like “Provided 8,198 tokens, but the maximum allowed is 8,192.” My log of tool calls is empty for failed requests, so I can’t show what filled the context. The successful moves used up to nine calls each, so repeated calls are the likely cause. That mode took 53 minutes of the run on its own.

Then it lost

Taking illegal moves away didn’t make it a tic-tac-toe player. With guided generation and with checkMove, it lost 18 games of 20. The forfeits turned into losses.

I counted the moves where the opponent had two in a row and the third square open. With guided generation the model blocked 7 of 24; with checkMove, 10 of 27. A few of those were forks, with two threats at once and no saving move (3 with guided generation, 5 with checkMove), but most weren’t. It knew which squares were free. It didn’t see the threat.

That fits what the other two posts found. The on-device model is good at reading what’s in front of it and filling in a shape. It isn’t good at working out what a position means, and a tool that answers “is this square empty?” doesn’t help with that.

What I got wrong

  • I wrote the first version of this post with the wrong explanation. I said that having to name a square and confirm it “seems to have been enough” to stop the illegal moves. An editor checking the logs found what was really happening: the model was taking the first square on the list. My own counts said it never asked about a taken square, and I didn’t ask why.
  • The tool modes had different instructions. Each told the model to call its own tool, and the other two modes had none. I can’t separate the tool from the instruction.
  • I didn’t log tool calls for failed requests, so the freeSquares loop is an inference from the successful moves, not something I saw.
  • freeSquares took an argument it didn’t need. I gave it a mark (X or O) so its Arguments wasn’t empty. I don’t know whether a tool with no arguments would have behaved differently.
  • My first test printed nothing. I piped the requests through the shell’s echo, which turned the \ns in the prompt into real line breaks and broke every line of JSON. And my first dynamic schema listed the square type twice, which the framework refused (“Duplicate type Move”). Both were caught before the real runs.
  • Small numbers, one run each. Twenty games per mode, and the model doesn’t always answer the same board the same way, so a second run would move these numbers around.

What I take from it

If your app needs Apple’s on-device model to pick from a known set of valid answers, constrain the answer. Guided generation was fast and never failed. Tools were slower, and both of mine went wrong: one changed the model’s strategy to something simple and losing, and the other sent half its requests into errors after minutes. If you give it tools, watch how many calls it makes, set a timeout, and check what it actually does with the results.

Neither will make the model reason better. Here, it stopped breaking the rules and kept losing.

If you constrain a model so it can’t give a wrong answer, how do you notice when it’s giving a bad one?

Related posts