---
title: "Grok 4.7 on Pydantic AI Transit Planner"
description: "Journey planning over a real Dresden public transport timetable, with an SVG map of each answer. Tool calling 90%, code mode 100%; rank 10 of 32."
canonical: https://models.pydantic.dev/models/grok-4.7/transit_planner/
---

# Grok 4.7 on Pydantic AI Transit Planner

> Journey planning over a real Dresden public transport timetable, with an SVG map of each answer. Tool calling 90%, code mode 100%; rank 10 of 32.
>
> Markdown copy of https://models.pydantic.dev/models/grok-4.7/transit_planner/. Index for agents: https://models.pydantic.dev/llms.txt

**Agents: use the API for model information.** It answers a question in one request, where the pages need several.

- Choose a model for an agent: `POST https://models.pydantic.dev/api/select` with a JSON body such as `{"description": "what the agent does"}`. It returns up to five ranked models with reasons. Format: https://models.pydantic.dev/select.md
- Search models by name: `GET https://models.pydantic.dev/api/search?query=<text>`
- List, filter and sort models: `GET https://models.pydantic.dev/api/models`, for example `?weights=open&max_price=2&sort=efficiency`
- One model in full: `GET https://models.pydantic.dev/api/models/<slug>`
- Any other question about the models: `GET https://models.pydantic.dev/api/ask?query=<question>`

Add `intent=<intent>&stack=<stack>&harness=<harness>` to the query string of all but the single model. An `OPTIONS` request to any endpoint returns its schema. The same five are tools of the MCP server at `https://models.pydantic.dev/mcp`. More: https://models.pydantic.dev/usage.md

About the benchmark: https://models.pydantic.dev/benchmarks/transit_planner.md. The model on every Pydantic AI benchmark: https://models.pydantic.dev/models/grok-4.7.md

| Mode         | Score | Tasks passed | Cost   | Requests | Tool calls | Time      |
| ------------ | ----- | ------------ | ------ | -------- | ---------- | --------- |
| Tool calling | 90%   | 9 of 10      | $19.48 | 615      | 734        | 81.0 min  |
| Code mode    | 100%  | 10 of 10     | $14.40 | 463      | 474        | 100.8 min |

Each task was run once per mode.

## Review

Written by an LLM from the runs' transcripts.

#### Overall

`gateway/openrouter:x-ai/grok-4.7` passed 19/20 runs: 9/10 in tools and 10/10 in code mode. The sole failure was request exhaustion on `tour`, not a provider failure. These results favor code mode for larger routing problems, although neither mode is consistently economical.

The returned Logfire arrays end before completion for tools `departure_profile` and code-mode `after_midnight` and `one_change`; their accepted outcomes come from the supplied results, with implementation claims limited to recorded steps.

#### Where it does well

It handles timetable details that readily break journey planners: previous-service-day offsets, repeated run identifiers, distance-based interpolation, boarding restrictions, walking access and transfer margins. Both modes pass all seven ordinary journey/meeting cases and both departure profiles.

Verification can be substantive. The tools `one_change` run correctly excludes a tram departing 17 seconds before transfer readiness. Code-mode `tour` catches an interpolation defect, matches a 48,428-call checksum against SQL and reruns the search before submission.

#### Mistakes it makes

Unquoted `at` causes SQL parser errors in 18 recorded conversations. Several repairs then assume a failed multi-statement batch created its tables. Other recurring costs include repeated schema exploration, individual alternative checks and rebuilding routing tables.

Code adds its own pitfalls: unsupported timing functions in two cases, custom heap behavior failures in `saturday_direct`, and mutable interpolation anchors in `tour`. The timed-tour code tests only eight of 24 visit orders, although its chosen answer passes.

Tools `tour` spends most of its budget on a restricted direct/one-transfer model, finds `49560`, then begins a broader search too late. Code mode completes at `48660`, 900 seconds earlier than that unfinished candidate.

#### Tool calling compared with code mode

Tools consumed 615 requests and $19.4782; code mode consumed 463 and $14.3990: approximately 25% fewer requests and 26% lower cost. Code uses fewer requests in eight pairs, but costs less in only seven.

The largest gains are `timed_tour` (75 → 33 requests), `tour` (80 and failure → 48 and pass), and `departure_profile` (78 → 50). Simple cases show less benefit: `one_vehicle` and `one_change` require more requests in code mode. Equal objective values can also yield different convenience: code-mode `after_midnight` adds a vehicle without improving arrival.

## Runs

### one_vehicle

**Tool calling: passed.** Passed by walking to Strehlener Platz and taking only `R27f8d4b1+2`, boarding at `29940` and arriving at `30480`. ($0.65, 30 requests, 7.5 min)
- Right: Searched nearby platforms rather than restricting boarding to Lennéplatz.
- Right: Verified the 399-second walk using `ceil(sqrt(9*9 + 398*398))`, and checked the repeat offset: `27540 + 2*1200 = 29940`.
- Right: Kept the walk out of the vehicle-leg list.
- Mistake: An early `run_sql` query failed at `CASE WHEN at = ''`; quoting `"at"` fixed it.
- Mistake: The final `departure` was `29400`, although the vehicle departure minus the verified walk is `29541`. The result passed, but its stated departure includes 141 seconds of avoidable waiting.
- Mistake: Wrote the SVG three times before calling `final_result`.

**Code mode: passed.** Passed with the same bus as tool mode, arriving at `30480`, and a precisely calculated set-off time of `29541`. ($0.67, 34 requests, 5.6 min)
- Right: Combined SQL candidate discovery with Python interpolation and repeat evaluation.
- Right: Calculated `ceil(sqrt(158485)) = 399` and `29940 - 399 = 29541` for the initial walk.
- Right: Returned exactly one vehicle leg and depicted the walk separately.
- Mistake: An early query failed on unquoted `at`.
- Mistake: A later query used the reserved word `both` as a CTE name and failed; a nonreserved name or quoted identifier was needed.
- Mistake: Used 34 requests, compared with tool mode's 30, despite the identical selected vehicle.

### saturday_direct

**Tool calling: passed.** Passed with direct tram `Rb28d92d7`, departing at `33484` and arriving at `34560`. ($1.10, 45 requests, 7.3 min)
- Right: Filtered Saturday services and checked the selected run's service membership.
- Right: Explicitly calculated the untimed boarding call as `33240 + floor(300 * 1385 / 1696) = 33484`.
- Right: Tested direct, walking-access and transfer alternatives before writing the SVG and final result.
- Mistake: Two consecutive attempts to create `calls` failed because `at` remained unquoted, including the attempted repair using `NULLIF(at, '')`.
- Mistake: The direct candidate was established relatively early, but extensive subsequent two- and three-vehicle checks brought the total to 45 requests.

**Code mode: passed.** Passed with `Rb28d92d7`, departing at `33484` and arriving at `34560`, after recovering from SQL and Python runtime errors. ($1.14, 42 requests, 9.9 min)
- Right: Implemented interpolation, Saturday trip expansion and a platform search including walks and transfers.
- Right: Used the known direct arrival as a bound while searching for a strictly earlier journey.
- Right: Generated a route path through intermediate stops with the required `data-run` and endpoint `data-place` attributes.
- Mistake: SQL preparation failed on unquoted `at` and on mixing `typeof(east)` with aggregates without grouping.
- Mistake: The first heap loop, `while heap:`, eventually unpacked `None` from an empty heap. Changing it to `while len(heap) > 0` also failed because the sandbox did not expose the custom length behavior.
- Mistake: It finally used `while heap.a:`. Checking the backing list initially would have avoided both runtime failures.

### one_change

**Tool calling: passed.** Passed with exactly two vehicles: `Rafeef850` at `27669`, followed by `R620ec7d9`, arriving at `28440`. ($1.21, 58 requests, 8.6 min)
- Right: Interpolated the first boarding time and enforced the vehicle limit through first-leg and second-leg SQL searches.
- Right: Verified that the transfer requires `27960 + 72 + 120 = 28152`. This excludes `R45005fd8+4` at `28135` but permits `R620ec7d9` at `28200`.
- Right: Checked alternative arrivals and wrote the required platform and run markers.
- Mistake: The initial call query failed on unquoted `at`.
- Mistake: A later interpolation inspection selected ambiguous `east` and `north` columns after a join; the query needed qualified column names.
- Mistake: After establishing the winning connection, numerous individual departure and platform checks extended the run to 58 requests.

**Code mode: passed.** Passed with the same two-vehicle itinerary as tool mode and arrival `28440`, but used 65 requests. The returned conversation ends after finding 56 feasible pairs, before finalization. ($1.64, 65 requests, 11.1 min)
- Right: Built numeric stop sequences, interpolated times and repeat offsets with `run_sql` inside `run_code`.
- Right: Checked that timed calls had zero arrival/departure mismatches.
- Right: The last recorded query applies `dep2 >= transfer_arr + 120 + xfer_walk` and reports `MIN(arrival) = 28440` across 56 feasible pairs.
- Mistake: Two untimed-call counts repeated the unquoted-`at` syntax error.
- Mistake: A later inspection requested `"at"` from a derived table that exposed `arr` instead; it recovered by joining the raw calls.
- Mistake: The supplied request count exceeds tool mode by seven, so code mode provided no request-efficiency benefit here.

### walking_change

**Tool calling: passed.** Passed with bus `R76d2117b+4`, a walk from Prager Straße to Altmarkt, and tram `R3a0dccd4+54`, arriving at `63480`. ($1.89, 75 requests, 11.2 min)
- Right: Calculated the inter-platform distance as approximately `340.401` metres and rounded the walk to 341 seconds.
- Right: The connection is feasible: `61920 + 341 + 120 = 62381`, before the interpolated tram departure at `62458`.
- Right: Checked repeat offsets `4*1200 = 4800` and `54*600 = 32400`, and verified `YY` access on all selected calls.
- Mistake: Loading and inspection produced unquoted-`at` parser errors, an invalid comparison of a numeric field with `''`, and a subsequent query against nonexistent `raw_calls`.
- Mistake: The SVG-coordinate query selected ambiguous `name` after a join.
- Mistake: Repeated routing rounds and individual checks consumed 75 requests despite finding the final two-vehicle connection earlier.

**Code mode: passed.** Passed with bus `R76d2117b+4` and tram `R3a0dccd4+54`, departing at `61560` and arriving at `63480`. It transferred at Pirnaischer Platz rather than tool mode's Prager Straße–Altmarkt walk. ($1.43, 48 requests, 8.0 min)
- Right: Used SQL routing rounds with `arr + 120 + walk` readiness and investigated earlier destination services.
- Right: Selected bus alighting at `P6e794064` at `62040` and tram boarding at `P72ccec8b` at `62520`.
- Right: Verified both repeated runs and inspected the completed SVG for their `data-run` attributes.
- Mistake: Two preparation queries failed on unquoted `at`, including a summary appended to table creation.
- Mistake: Continued querying alternative arrivals and nearby platforms after writing the SVG; it still finished comfortably within the limit at 48 requests.

### after_midnight

**Tool calling: passed.** Passed with two vehicles, departing at `5280` and arriving at `6420`. It combined previous-day services, repeat offsets and an interpolated stop time correctly. ($2.15, 64 requests, 3.7 min)
- Right: Used `read_text` to read `/data/DATA.md`, then `run_sql` to build interpolated calls and Friday/Saturday service instances.
- Right: Explicitly verified `91680 - 86400 = 5280` for `R93fefd15`. The transfer required `5492 + 76 + 120 = 5688`, leaving 12 seconds before `R900bf42f+2` departed at `5700`.
- Right: Wrote `/out/journey.svg` with the two run identifiers and boarding/alighting platform markers.
- Mistake: During table creation, two `run_sql` calls failed on unquoted `at`, first in `CASE WHEN at = ''` and then in `at IS NULL`. Quoting `"at"` and renaming it `arr` resolved this.
- Mistake: After finding the two-vehicle arrival at `6420`, it spent many additional queries inspecting competing night services and repeating service, interpolation and connection checks; the run consumed 64 requests.

**Code mode: passed.** Passed with three vehicles, departing at `4380` and arriving at `6420`. The recorded message array ends during timetable preparation, so the later routing and finalization are supported only by the supplied result. ($2.05, 60 requests, 3.0 min)
- Right: Read `/data/DATA.md` through `run_code` and used `run_sql` to inspect both Friday and Saturday service eligibility.
- Right: The accepted output uses `Rca4e4824`, `R93fefd15` and `R900bf42f+2`, with times expressed relative to Saturday.
- Mistake: The recorded preparation fails on `CAST(at AS INTEGER)` and later on `LAG(dep_raw) IGNORE NULLS OVER w`. The latter needed DuckDB's null-handling syntax inside the function arguments.
- Mistake: The accepted itinerary leaves 900 seconds earlier and uses one more vehicle than tool mode while reaching the same `6420` arrival. This is not a failure of the earliest-arrival objective, but it is a less convenient tie choice.

### arrive_by

**Tool calling: passed.** Passed with the latest departure, `63360`, using `R1aabe4da+29` and `Rfd6f9e2d+67`, arriving at `64440` before the `64800` deadline. ($1.63, 58 requests, 7.5 min)
- Right: Built origin/destination walking access and searched connections with the `120 + walk` transfer requirement.
- Right: Investigated departures later than `63360`, including additional transfers, rather than treating the first feasible itinerary as the latest departure.
- Right: Verified the selected runs, access flags, repeat offsets and SVG coordinates before `final_result`.
- Mistake: The service-expansion batch failed at `MIN(at)`. A later query assumed `valid_services` existed and produced a `CatalogException`; the failed batch needed to be recreated first.
- Mistake: The monotonicity check put `LAG(dep)` inside `SUM(...)`, which DuckDB rejected. It recovered by calculating the window expression in a CTE before aggregating.

**Code mode: passed.** Passed with departure `63360` and arrival `64500`, using `R1aabe4da+29` and `R54c1ee76+5`. ($1.26, 46 requests, 11.6 min)
- Right: Fetched SQL results in 200-row pages and built Python run-stop structures for the backward latest-departure search.
- Right: Cross-checked the result with a forward search restricted to origin departures after `63360`.
- Right: Wrote and inspected the SVG before `final_result`.
- Mistake: An early untimed-call count failed on unquoted `at`.
- Mistake: The selected arrival is 60 seconds later than tool mode's result at the same departure time. Both pass because latest departure is the requested objective; the code-mode search did not select the earlier-arriving tie.

### departure_profile

**Tool calling: passed.** Passed with an accepted departure profile and SVG, using 78 requests. The returned Logfire message array contains only the opening 13 messages, so the later search and finalization cannot be audited from that conversation. ($2.86, 78 requests, 15.8 min)
- Right: The recorded opening reads `/data/DATA.md` in three `read_text` calls and queries the requested station platforms, services and service changes with `run_sql`.
- Right: The supplied accepted output includes profile points `25327 → 27420`, `25620 → 27828` and `25927 → 28020`, with explicit run and platform identifiers.
- Mistake: No specific implementation error is visible in the recorded opening. At 78 requests, the completed run had only two requests of headroom under the 80-request limit; the missing later messages prevent attributing that cost to particular steps.

**Code mode: passed.** Passed with 12 journeys, from `25327 → 27420` through `28620 → 30780`, in 50 requests. ($1.93, 50 requests, 13.7 min)
- Right: Used SQL to build transfer candidates and a descending-departure suffix minimum to remove dominated journeys.
- Right: Corrected the dominance expression to compare against strictly later departures: `ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING`, with `jarr < best_later`.
- Right: Implemented `raptor(set_off, max_rides=4)` and checked departure boundaries and the following second, including `25327 → 27420` versus `25328 → 27828`.
- Mistake: Two preliminary queries failed on unquoted `at`.
- Mistake: A timing experiment called unsupported `time.time()` and failed before the file read.
- Mistake: The first exploratory dominance check included the current row and used `<=`, retaining earlier departures with equal arrivals. It repaired this before producing the final profile.

### meeting_point

**Tool calling: passed.** Passed by choosing Albertplatz, `Sbb9212fa`, at `33701`. The two direct journeys arrived at `33701` and `33692`. ($1.72, 52 requests, 11.4 min)
- Right: Used repeated `run_sql` routing rounds for both origins, including walking access and transfers, then compared station arrival times.
- Right: Checked the interpolated arrivals for `Rc0124950` and `Rfa439a34`; the meeting time correctly equals the later arrival.
- Right: Returned one vehicle leg for each friend and marked all four used platforms in the SVG.
- Mistake: During schema exploration, three queries failed on unquoted `at`: `at IS NULL`, `typeof(at)` and `SELECT at, dwell`. It should have quoted the field after the first parser error.
- Mistake: Wrote `/out/journey.svg` twice before finalizing; the run used 52 requests for a result containing two direct rides.

**Code mode: passed.** Passed with the same Albertplatz meeting and journeys as tool mode, at `33701`, using 37 requests. ($1.11, 37 requests, 9.3 min)
- Right: Built expanded Saturday trips and ran the reusable Python `route(origin_station)` function for both friends.
- Right: Compared station arrivals and checked that no station was reachable by both before `33701`.
- Right: Independently inspected interpolation for the selected direct runs and verified the SVG's required markers.
- Mistake: The `CREATE TABLE calls` batch failed on unquoted `at`; the next call assumed the table existed and failed again.
- Mistake: Called unsupported `time.perf_counter()`, producing a runtime error.
- Mistake: Added background dots for stations across the map, inflating the supplied SVG output to roughly 77,000 characters for a two-leg answer.

### timed_tour

**Tool calling: passed.** Passed with five vehicle legs and a return at `44160`, visiting Prohlis, TU, Pohlandplatz and Mickten in that order. ($2.85, 75 requests, 1.9 min)
- Right: Returned visits satisfying the opening windows and minimum stays, including Mickten from `42300` to `43260`.
- Right: Accounted for the 117-second walk before the Prohlis departure: `35640 - 117 = 35523`, giving a stay of 1083 seconds.
- Right: Checked run access, stop order, repeat offsets and inter-platform distances before writing the SVG.
- Mistake: Two SQL batches failed on unquoted `at`; a follow-up then queried the nonexistent `calls` table instead of recreating the failed batch.
- Mistake: A later join failed because `run_id` was ambiguous.
- Mistake: Repeatedly rebuilt routing tables and inspected individual alternatives, consuming 75 requests and leaving little budget margin.

**Code mode: passed.** Passed with the same five-leg tour and return time `44160` as tool mode, using 33 requests. ($1.34, 33 requests, 13.4 min)
- Right: Implemented reusable routing and `min_setoff`, enforcing `max(arrival + 900, open_t + 900)` and rejecting arrivals after `close_t - 900`.
- Right: Calculated actual visit departures from the next vehicle minus its initial walk; the four stays were 1083, 1232, 1080 and 960 seconds.
- Right: Independently checked the final Mickten-to-Pirnaischer segment and interpolated boarding time `38312`.
- Mistake: The order-search code explicitly listed only eight of the 24 possible visit permutations. The winning order was included and passed, but the recorded search did not exhaustively enumerate all orders or provide a programmatic pruning proof for those omitted.

### tour

**Tool calling: error.** Failed at the 80-request limit without a final answer or SVG. Its restricted search found a return at `49560`, then it started a broader label search too late to finish. ($3.41, 80 requests, 6.1 min)
- Right: Detected that stop sequence `n` was being sorted lexically and rebuilt interpolation with `CAST(c.n AS INTEGER)`.
- Right: Enumerated all 120 visit orders using its direct/one-transfer journey table and reconstructed a concrete candidate.
- Right: Verified the candidate's boarding permissions, stop order and walking distances.
- Mistake: The initial interpolation ordered string-valued `n`, producing incorrect stop ordering; numeric conversion should have preceded all window calculations.
- Mistake: The main journey table allowed at most one transfer between visits and filtered runs through target access platforms. This omitted possibilities such as the three-vehicle Julius-to-Altpieschen segment used by the passing code-mode solution.
- Mistake: Only near the end did it create `lab(platform, mask, t)` and begin broader routing rounds. It stopped while comparing first-round Altpieschen arrival `37680` with the existing two-vehicle arrival `37440`.
- Mistake: It should have used a complete reusable routing search from the outset, with visit-mask states and predecessor tracking, rather than spending most of the request budget building and repairing a restricted pairwise search.

**Code mode: passed.** Passed with 12 vehicle legs and return time `48660`, using 48 requests. Its route visits Julius-Vahlteich, Altpieschen, Waldschlößchen, Marienberger Straße and Wasaplatz. ($1.82, 48 requests, 15.1 min)
- Right: Combined a connection scan with visit-mask search; the corrected search reported 79 expansions and best completion `48660`.
- Right: Allowed three vehicles between Julius-Vahlteich and Altpieschen, a possibility omitted by tool mode's initial pairwise model.
- Right: After correcting interpolation, compared all 48,428 arrival times by checksum: Python and SQL both returned `524552435` modulo `1000000007`, then reran routing and reconstructed the legs.
- Mistake: An early SQL count failed on unquoted `at`.
- Mistake: The initial Python interpolation reused already-filled stops as timed anchors by searching mutable `arrs` and `deps`. This can accumulate flooring errors across consecutive untimed stops.
- Mistake: It discovered the interpolation defect during selected-leg verification, introduced an immutable `is_timed` mask, recomputed 14,614 untimed calls and reran the search before finalizing.
