https://withleaf.io/inference/v1, authenticate with a Leaf V2 API key from dashboard.withleaf.io (or a Keycloak OAuth2 access token), and call Leaf Router 1 or any of 25 frontier open-weight and closed models at 25% below maker list price — with zero data retention and optional live access to Leaf soil, weather, satellite imagery, and machine data.
You can also try models and live tools in the interactive playground at withleaf.io/inference.
Base URL and endpoints
For full request/response schemas, streaming SSE chunk formats, and error codes, see the Inference API Reference.
Quickstart
Create an API key in dashboard.withleaf.io (API Keys → Create API key) to get aclient_id (starting with leaf-api-) and client_secret, then set LEAF_API_KEY="<client_id>:<client_secret>". Point any OpenAI-compatible SDK at https://withleaf.io/inference/v1 and set model to leaf-router-1 to automatically route every agronomy question to the best zero-data-retention model.
Authentication
Leaf Inference authenticates callers exclusively against Leaf’s V2 Keycloak realm (https://auth.withleaf.io/realms/leaf) and routes all built-in Leaf data tool calls to https://api-v2.withleaf.io. Legacy V1 tokens are not accepted. Two V2 credential formats are supported:
Option 1: Leaf API key (recommended for OpenAI SDKs)
- Open dashboard.withleaf.io, select your project, and open API Keys → Create API key.
- Copy the generated
client_id(leaf-api-...) andclient_secret(shown once). - Pass
<client_id>:<client_secret>directly as yourapi_keyin any OpenAI SDK (Authorization: Bearer <client_id>:<client_secret>) or in theX-API-Keyheader:
POST https://auth.withleaf.io/realms/leaf/protocol/openid-connect/token with grant_type=client_credentials), caches the short-lived OAuth2 access token until 30 seconds before expiry, and uses it for any leaf_tools calls against https://api-v2.withleaf.io — so you do not need to manage 5-minute token refreshes in your application.
Option 2: Keycloak OAuth2 access token
If your service already obtains OAuth2 tokens fromPOST https://auth.withleaf.io/realms/leaf/protocol/openid-connect/token (client_credentials, password, or refresh_token grants — see Authentication), pass the access_token directly:
https://auth.withleaf.io/realms/leaf/protocol/openid-connect/certs), checks token expiry, and forwards the token to https://api-v2.withleaf.io when leaf_tools is enabled.
Multi-project API keys (X-Leaf-Project)
If you created a multi-project API key in the dashboard (Selected projects or All my projects), pass the optional X-Leaf-Project: <api_owner_username> header to target a specific project for leaf_tools calls on https://api-v2.withleaf.io.
GET /inference/v1/modelsis public and does not require authentication.- When
leaf_toolsis enabled, the model can only access growers, fields, satellite monitoring, weather, and machine operations authorized for your API key or token (including read-only and project scope restrictions). - The interactive web playground at withleaf.io/inference allows keyless same-origin requests with tighter limits (6 requests/minute, point-based soil and weather tools only, no custom
tools). External API callers must always authenticate.
Leaf Router (leaf-router-1)
leaf-router-1 is Leaf’s agronomy-tuned automatic router. It scores 82.9% overall on Agronomy Benchmark v3 (data from Agronomy Arena) at $2.52 per 1,000 benchmark answers — beating every individual frontier model on accuracy while cutting per-answer cost by up to 20x compared to leading reasoning models.
How routing works
On each request withmodel: "leaf-router-1", Leaf inspects recent conversation context before dispatching the completion:
- Context inspected: Up to the last 4
userandassistantturns (up to 2,400 characters per message). Callersystemprompts are forwarded intact to the selected model without distorting routing classification. - Multimodal vision routing: When the conversation includes images (
image_urlorinput_imagecontent parts), Leaf inspects up to the 3 most recent images alongside the text to classify agricultural subjects (plant disease/lesions, weed species, insect/arthropod pests, or general field photos) and visual difficulty. - Task and difficulty classification: For text requests, Leaf detects task structure (including explicit multiple-choice options such as
A) / B) / C)), classifies whether the prompt involves general agronomy, EPA pesticide label specifications, or quantitative university extension thresholds, identifies the agronomic domain (crop, nutrient, pest, soil/water, or grower operations), and estimates difficulty. - Zero data retention and failover: Both the routing step and the selected model strictly enforce zero data retention (
zero_data_retention: true) and deny provider data collection. If classification times out (3.5stimeout) or an upstream provider is unavailable, Leaf falls back automatically so requests complete reliably. - Opaque response contract: Responses always return
"model": "leaf-router-1". Resolved internal routing targets are never exposed in responses, headers, or errors.
leaf-quality-router-1 is accepted as an alias for leaf-router-1. Use leaf-router-1 in new integrations.System prompts
Leaf Inference supports both caller-supplied system prompts and automatic tool system prompts when Leaf data tools are enabled.Caller-supplied system prompts
Include one or more messages with"role": "system" inside messages, following the standard OpenAI Chat Completions format:
- Your
systemmessages are passed through verbatim to the model. - When using
leaf-router-1, routing classification reads onlyuserandassistantturns, so detailed persona, formatting, or compliance instructions in yoursystemprompt do not interfere with model selection.
Automatic system prompt when leaf_tools is enabled
When you enable one or more groups via leaf_tools or X-Leaf-Tools, Leaf prepends a concise system instruction ahead of your messages array. Your own system message(s) remain intact right after it.
Specifically, Leaf’s prepended system prompt instructs the model to:
- Use the enabled Leaf data tools (
weather,soil,imagery,machine) whenever a question requires live agricultural data. - Cite the exact dates, locations/coordinates, and units returned by the tools, and state plainly when data is missing or unavailable.
- For authenticated requests, discover account fields first with
leaf_list_fields(andleaf_list_growersif needed) before calling field-scoped tools. - Respect which Leaf data groups are switched off for the current request instead of fabricating unavailable data.
Recommended system prompt patterns
- Agronomy advisor
- Label & compliance
- Structured extraction
Supported parameters and default values
The table below lists every request body parameter and header supported byPOST /inference/v1/chat/completions, along with its default value and server-side bounds.
Request body parameters
Request headers
Server-enforced parameters: Only the parameters listed above are forwarded to models. Unrecognized fields and provider-routing overrides are stripped server-side so Leaf always enforces zero data retention, privacy controls, and automatic failover. Usage accounting is always requested so
usage totals are populated.Built-in Leaf agronomy tools (leaf_tools)
Switch on live agricultural data per request with "leaf_tools" in the JSON body (OpenAI Python SDK: extra_body={"leaf_tools": [...]}) or with the X-Leaf-Tools header.
Tool groups
Enable all four groups at once with
"leaf_tools": true or X-Leaf-Tools: all. Whenever any group is enabled on an authenticated request, Leaf also includes leaf_list_growers and leaf_list_fields automatically so the model can resolve grower and field names to IDs.
Available server-side tools
Execution behavior and combining with your own tools
- Server-side execution loop: Leaf executes built-in tool calls server-side for up to 6 rounds per completion and merges token
usageacross rounds. - Compaction and truncation: Weather and soil responses are compacted into columnar arrays; hourly series longer than 72 hours (3 days) are thinned to every 3 hours (
"resolution": "every 3 hours"). Individual tool outputs are capped at 30,000 characters. - Transparency in responses:
- Non-streaming: Responses include a top-level
leaf_tool_callsarray ([{ "name", "arguments" }]) whenever one or more Leaf tools ran. - Streaming: Leaf emits progress chunks carrying
leaf_toolmetadata (status: "running", then"done"or"error") alongside standard OpenAI delta chunks.
- Non-streaming: Responses include a top-level
- Mixing with custom caller
tools: You can send your own OpenAItoolsalongsideleaf_tools. Built-inleaf_*tool names are reserved whenleaf_toolsis enabled. If a model emits any caller-defined tool call in a turn, Leaf returns yourtool_callsimmediately withfinish_reason: "tool_calls"so your application stays in control (any Leaf calls emitted in the same turn are dropped).
Available models and pricing
Every model in Leaf Inference is served at 25% below its maker’s list price. QueryGET https://withleaf.io/inference/v1/models programmatically for the live catalog and pricing.
Privacy, zero data retention, and limits
- Zero data retention (ZDR): 24 of 26 catalog models — including
leaf-router-1— pin every request strictly to zero-data-retention endpoints (zero_data_retention: true). All 26 models prohibit provider data collection and training, and Leaf never logs prompts or outputs. - Anonymous user hashing: Leaf passes only a one-way SHA-256 account hash for abuse prevention, never raw grower emails or account IDs.
Rate and payload limits
What to do next
- Inference API Reference — Full endpoint reference, streaming SSE event formats, response schemas, and error codes.
- Authentication — Obtain your Leaf Bearer token.
- Weather Overview — Details on weather models, soil moisture depth layers, and unit systems.
- MCP Server — Connect Leaf directly to Cursor, Claude Code, and other MCP clients.

