cancel
Showing results for 
Search instead for 
Did you mean: 
Community Articles
Dive into a collaborative space where members like YOU can exchange knowledge, tips, and best practices. Join the conversation today and unlock a wealth of collective wisdom to enhance your experience and drive success.
cancel
Showing results for 
Search instead for 
Did you mean: 

I Built Tetris in One Databricks Function, Then Let ai_decide and LLMs Play It

suranga
Databricks Partner

Why Tetris

I wanted to know if one Databricks function can play Tetris, and what Unity Gateway sees when an AI agent hammers it move after move.

It started with the ai_decide announcement by Jane Zhang, Matthew Ding and Archika Dogra. Their demo is Snake on Databricks Apps: on every tick the board goes to ai_decide, and it picks the next direction. I loved it. Then I thought: Snake has a few directions. Tetris has up to 34 legal drops per piece, holes you can never fix, and a stack that kills you if you get lazy.

The same month, Unity Gateway got a pile of changes (next section). I wanted a real workload to push through it, not a hello world.

So I put the whole game in one Unity Catalog function and let ai_decide, a classic bot and a bunch of LLMs play it in my own Azure workspace. This is purely my experience from my personal sandbox (no client data), so your numbers will differ.

What I wanted to find out:

  1. Can ai_decide play Tetris, and how fast is it?
  2. Do LLMs do better, and at what cost?
  3. What does Unity Gateway really record and enforce when an agent calls it hundreds of times?

What changed in Unity Gateway in October

October was a big month: the docs now call it Unity Gateway (the workspace sidebar still says AI Gateway), and almost every week brought governance features for agents, not just models.

DateWhat changedStatus
28 Octug usage in the Unity Gateway CLI shows your personal and shared spend limitsNot stated
27 OctClaude, Claude Code, ChatGPT, Codex and Cursor can connect to Databricks MCP servers without an admin creating an OAuth appGA
27 OctBuilt-in usage dashboard: spend per model and user, coding agents, MCPs, skills, tag attributionNot stated
26 OctSpend Status API for Unity Gateway budgetsNot stated
21 OctThe Jailbreak guardrail can screen MCP tool calls and their resultsBeta
20 Octai_enrich can use an external MCP search tool registered in Unity GatewayBeta
15 OctConsumer-access users can query Unity Gateway services, with budgets and rate limitsGA
12 OctSkills API and developer toolsBeta
9 OctRequest tags inside service policies, and OpenJev as the LLM-as-a-judge evaluatorBeta
9 OctAmazon Bedrock Mantle as a model providerNot stated
7 OctExternal partner guardrails as service policiesBeta
7 to 27 OctNew models: Claude Haiku 5.5, Gemini 4 Argon, DeepSeek V4.1 Flash (1017), GPT-6.1 TerraNew

Source: Databricks release notes, October 2026. ai_decide itself is older: Beta since 30 September.

The whole game in one function

The entire game is one Unity Catalog Python function, play(state, move): you pass the current state as JSON plus a move, and it returns the next state. No app, no cluster to keep alive.

02_chart_how_the_race_is_wired.png

How the race is wired: three kinds of players, one function

The game loop asks a player for a drop and hands the move to play(); only the LLM calls go through the Unity Gateway unified API with request tags.

What's inside, about 200 lines of plain Python with only the standard library:

  • A fair game. A 10 x 20 board, the 7 classic pieces and a seeded "7-bag" randomizer. Same seed, same pieces, every run, so every player gets exactly the same game.
  • Drops, not key presses. Each turn the function lists the legal drops as labels like r1_c4 (rotation 1, leftmost column 4), with a short description of each. Up to 34 per piece.
  • Line clears and classic scoring. 100, 300, 500 or 800 points for 1 to 4 lines.
  • A built-in bot. play(state, 'AUTO') places the piece with Pierre Dellacherie's classic features and the El-Tetris weights. In my local test it played 500 pieces and cleared 197 lines without dying. It's the baseline everyone else has to beat.
  • Bad answers don't crash anything. An unknown label counts as an invalid move and the piece drops in the middle, like a player who missed the button.

Three calls are all you need:

SELECT play(NULL, 'NEW 7');      -- start a game with seed 7
SELECT play(:state, 'AUTO');     -- let the bot place the piece
SELECT play(:state, 'r1_c4');    -- play a move

03_play_function_in_catalog.png

The play function in Catalog Explorer: one Python function, the whole game

Two things I only saw in my workspace. The first call took about 30 seconds in both runs (a cold start). Warm calls still took 3 to 4 seconds each, and with 11 game sessions running at once the median was 5.5 seconds per move. My one-function engine was slower than most of the AI players. And Catalog Explorer showed the function as Deterministic: False, even though it is deterministic, so v2 declares it DETERMINISTIC.

Round 1: ai_decide alone

With only the board to look at, ai_decide topped out after 19 pieces with zero lines. With a short description of each drop, it survived all 40 pieces and cleared 6 lines, where the bot cleared 14.

In my first run no LLM could play (next section), so it became ai_decide against the bot, in two rounds:

  • Raw: the board plus the list of legal drops (rotation and column).
  • Features: the same, but each drop also says what it does: lines cleared, holes left, max height and bumpiness.
PlayerRoundPiecesLinesInvalid movesSame pick as the botDecision time p50p95
Classic botraw40140100%under 1 msunder 1 ms
ai_decidefeatures406145%624 ms1,676 ms
ai_decideraw19, topped out015.3%626 ms1,688 ms

What I learned:

  1. It can't read a Tetris board. In the raw round it piled pieces in the middle, and only 1 of its 19 picks matched the bot.
  2. Describe the options and it plays like a careful beginner. Same model, same pieces, but the features round agreed with the bot on 18 of 40 moves.
  3. It is fast. About 0.6 seconds per decision over the REST API. The same call through SQL took 5.6 seconds in my probe: fine for batch scoring, too slow for a game loop.
  4. No token counts come back. The response has the choice, the probabilities and a confidence, but no token counts, so the cost has to come from billing.
  5. One call failed in each game (turn 7 and turn 8). Both were HTTP 429: "Rate limit exceeded after multiple attempts". The notebook counted each as an invalid move.

The real lesson isn't about Tetris: ai_decide judges options you describe. The quality came from how I described the choices, not from the board.

Docs vs reality: the models I could actually use

My workspace runs on a Databricks demo account, and none of the four models announced in October could play there: three aren't there at all, and the fourth is listed but blocked.

  • Gemini 4 Argon, DeepSeek V4.1 Flash (1017) and GPT-6.1 Terra are not on the Unity Gateway Models page. Calling them returned 404 ENDPOINT_NOT_FOUND.
  • Claude Haiku 5.5 is on the page, but the call returned 403 PERMISSION_DENIED: Access to this model is disabled for your workspace.
  • What is there: Claude Sonnet 5.5, Opus 5.5, Opus 5, Sonnet 5 and Opus 4.8, Qwen3.5 122B, GPT OSS 120B and 20B, Llama 4 Maverick, Llama 3.3 70B, Llama 3.1 8B and Gemma 3 12B.

05_unity_gateway_models.png

The Unity Gateway Models page on my demo account: the sidebar says AI Gateway, the title says Unity Gateway

That's on my side: it's a demo account, and it doesn't have every model switched on. Still a good reminder that "now available on Unity Gateway" in the release notes doesn't mean it's on for your account. Open your own Models page before you plan anything around a new model.

Small things I noticed on the way:

  • The ai_decide preview is called AI Decide (Beta). It was off; I had to switch it on under Previews.
  • The sidebar says AI Gateway, the page title says Unity Gateway, and a banner says model services now live in Unity Catalog.
  • A schema in Catalog Explorer now lists Services and Skills next to Tables and Functions.
  • The Gemma 3 12B card is labelled "Google Gemini model".

So the LLM race below runs on the models my account actually has.

The LLM race

With move descriptions, GPT OSS 120B cleared 14 lines and Llama 4 Maverick 12, the same level as the classic bot. On the raw board, no AI player cleared more than one line.

06_chart_lines_cleared.png

Lines cleared in one 40-piece game. Light bar: raw board. Dark bar: board plus move descriptions.

Run 2 played 11 game sessions in 4.8 minutes, all on the same seeded pieces. Claude was blocked on my demo account, so four open models played, plus ai_decide.

PlayerRoundPieces placedInvalid movesSame pick as the botMedian decisionOutput tokens
GPT OSS 120Bfeatures40060%1.6 s18,232
Llama 4 Maverickfeatures40047.5%0.26 s200
Gemma 3 12Bfeatures35, topped out127.8%0.20 s216
ai_decidefeatures35, topped out920%0.39 snone reported
Qwen3.5 122Bfeatures11, topped out120%12.2 s24,576
GPT OSS 120Braw14, topped out1213.3%8.0 s27,038
Llama 4 Maverickraw24, topped out112%0.26 s125
Gemma 3 12Braw25, topped out00%0.20 s150
ai_decideraw20, topped out59.5%0.41 snone reported
Qwen3.5 122Braw11, topped out120%12.1 s24,576

What I learned:

  1. Describing the moves beat reading the board. Same models, same pieces: GPT OSS 120B went from 1 line to 14 and Llama 4 Maverick from 0 to 12.
  2. Lines and score are not the same thing. GPT OSS 120B scored 1,600 against the bot's 1,400 with the same 14 lines, because it cleared two doubles.
  3. Fast and light can be good enough. Llama 4 Maverick decided in about a quarter of a second and wrote 200 output tokens for 40 moves, and still cleared 12 lines. GPT OSS 120B was a bit better but thought for 1.6 seconds per move and wrote 18,232 tokens.
  4. Give reasoning models room. Qwen3.5 122B (reasoning always on) spent my whole 2,048-token budget on every call and never answered: 0 valid moves in both rounds. GPT OSS 120B did the same on the raw board, with 12 of 15 answers empty. That's my cap more than the models, and it's exactly how this fails in a real agent.
  5. ai_decide got rate limited. 14 of its 56 calls came back 429 RESOURCE_EXHAUSTED, and my notebook only retried the LLM calls. Every 429 became a wasted drop. In run 1, with a single error, it had cleared 6 lines.

Governance tests

A per-user limit of 5 requests per minute stopped my agent loop after exactly 5 calls, but the same burst through ai_query went straight through, and my custom policy never fired.

08_chart_rate_limit_burst.png

Same model, same minute: the unified API was throttled, ai_query was not

Rate limits

  • Where they live: the model's page in Unity Gateway has a "Governance setup" panel with four tasks: usage tracking (on by default), inference table, rate limits and policies (Beta). There is no separate Rate limits tab.
  • What you can set: requests and tokens per minute or per hour, for the whole endpoint, per principal by default, and as overrides for a user, group or service principal.
  • Two kinds of 429 in one burst. Two calls hit a built-in "workspace QPS rate limit for databricks-gpt-oss-120b" before my limit kicked in, then five hit "User defined rate limit(s) exceeded". The built-in one still uses the old endpoint name and suggests provisioned throughput. Your agent has to handle both.
  • 429s are fast. They came back in 66 to 86 ms, so backing off and retrying costs almost nothing.
  • ai_query is not limited. 12 of 12 answered in 4.2 seconds, right after the API was throttled. The form says so, but it means a per-user limit does not stop a burst that runs through SQL.

09_rate_limit_form.png

The rate limit form, with my per-user default of 5 requests per minute and the ai_query note

A policy that blocks untagged requests

I wrote a custom SQL function policy: deny any request without a project request tag.

CREATE OR REPLACE FUNCTION <catalog>.tetris.require_project_tag(event VARIANT)
RETURNS VARIANT
LANGUAGE SQL
RETURN CASE
  WHEN event:context.request_tags.project IS NULL
  THEN to_variant_object(named_struct('result', 'DENY', 'reason', 'No project tag, no tokens.'))
  ELSE to_variant_object(named_struct('result', 'ALLOW', 'reason', ''))
END;

Three minutes after I attached it, an untagged request still came back 200 OK, and neither response carried a databricks_service_policy object. The docs say a DENY comes back as HTTP 200 with exactly that object, so I can't confirm either way yet. Maybe it needed more time, maybe I missed a step.

10_policy_test_output.png

The policy test: both calls came back 200 OK with no policy object

The policy form showed a few things the docs don't:

  • Sensitive Data Detection can Deny or Mask ("replace detected sensitive data with placeholders such as [EMAIL_ADDRESS]"), while the docs say policies don't transform content.
  • A custom function policy has no Phase choice. Its execution flow shows it on both the request and the response.
  • New policies apply to "All account users" by default.
  • The default LLM-as-a-judge evaluator is Claude Sonnet 5.

What Unity Gateway saw

Unity Gateway logged every LLM call with my request tags and matched my notebook to the token. But ai_decide never showed up anywhere, and the notebook running the game used more DBUs than all the models together.

ModelSpend on the usage dashboardModel serving DBUsLines cleared, both roundsValid moves, both rounds
Qwen3.5 122B$0.141.6200
GPT OSS 120B$0.050.551543
Llama 4 Maverick$0.040.451264
Gemma 3 12B$0.010.13260

Spend includes the probe calls, and for GPT OSS 120B also the governance tests. All usage from 31 October.

  • Tags work. request_tags is a map<string,string>, and the race rows in system.ai_gateway.usage matched my notebook's token counts exactly. Every 429 retry is its own row, and records showed up within about an hour.
  • ai_decide is invisible. No rows in the usage table and no billing line that names it, even though every call sent the request-tag header. It may be free in Beta, or billed somewhere I couldn't see. Either way, it's outside what Unity Gateway shows you today.
  • Failed calls are logged too. The three models my account doesn't have still got usage rows under the names I asked for, and the blocked Claude calls are there with no tokens.
  • The most expensive player never made a move. Qwen3.5 122B used 1.62 DBU, 59% of all model DBUs, for zero valid moves. A reasoning model that never finishes still bills every token it thinks.
  • The notebook cost more than the models. Over the three days of testing, the serverless notebook used 6.36 DBU, all model serving 2.75 DBU and the SQL warehouse 0.82 DBU. The tokens were the cheap part.
  • A billing product the docs don't mention: AI_GATEWAY (0.0016 DBU), next to MODEL_SERVING. My workspace is in Azure West Europe, going by the SKU names.

12_usage_by_player.png

system.ai_gateway.usage grouped by my request tags: round, player, requests and tokens

The new usage dashboard takes one click: Govern > Create Usage Dashboard > Setup & publish. It has tabs for Overview, Spend, Coding Agents, MCPs, Skills, Performance and a README, and it put my model spend for the day at $0.23 for 306K tokens. Read it with care, though:

  • There's no request-tag filter, only a "Group Spend by Request Tag" chooser on the Spend tab.
  • The request-tag estimate said $0.08 for game = tetris, against $0.23 actually spent.
  • Refused calls (the 403s and 429s) don't count as invocations, so the counts are lower than the usage table.
  • Most charts labelled my 31 October data "Nov 02, 2026".

13_usage_dashboard_overview.png

The Unity Gateway usage dashboard, Overview tab

One open question: the dashboard shows 12 Anthropic calls averaging 178 tokens, and billing has 0.013 DBU on an Anthropic SKU, while every Claude call through the API returned 403. It looks like ai_query reached Claude when the API couldn't. I'm checking that before I believe it.

What I'd tell a team before putting agents on Unity Gateway

Govern the path, not just the model: the same model behaved differently through the unified API, through ai_query and next to ai_decide.

  1. Describe the options. Every player did better when each move said what it does. That's data design, not model choice.
  2. Try the smaller model first. Llama 4 Maverick played at a quarter of a second per move with almost no output tokens, and came within 2 lines of the best.
  3. Give reasoning models a budget they can finish in. Otherwise they cost money and return nothing.
  4. Know which door each call uses. Unified API: rate limits, policies and usage tracking. ai_query: usage tracking only. ai_decide: none of it in my tests, plus its own rate limit.
  5. Expect two kinds of 429, and back off. The built-in workspace limit can fire before yours. Retry ai_decide too.
  6. Tag every request. It's one header, and it made every token traceable to a game, a round and a player.
  7. Watch the compute around the model. My notebook used more DBUs than all the tokens, and the one-function engine took 3 to 5.5 seconds per move.
  8. Test governance features before you rely on them. My policy didn't fire in my test, and the form offers things (Mask) that the docs say it can't do.
  9. Check your own Models page. The release notes said four new models; my demo account had none of them.

Try it yourself

The whole experiment is two notebooks on serverless, and the model tokens for the day cost me $0.23.

  • Before you start: switch on the AI Decide preview (Beta) in your workspace Previews, and check which models your Unity Gateway Models page actually lists.
  • The race notebook creates the play() function, checks which players are available, runs the game sessions in parallel and writes every move to Delta.
  • The governance notebook runs the rate limit and policy tests and, an hour later, the usage and billing queries.
  • Fixes I'd make next time: retry ai_decide on 429, give reasoning models more than 2,048 output tokens, and run fewer game sessions at once so the engine isn't the bottleneck.

If you want the notebooks, let me know in the comments and I'll share them. This is purely my experience on a demo account, so if you try it, I'd love to hear your numbers.

Thanks to Jane Zhang, Matthew Ding and Archika Dogra for the ai_decide Snake demo that started all this, and to Pierre Dellacherie's features and the El-Tetris weights behind the classic bot.

Sources

0 REPLIES 0