Custom Agent Integration with The Grid | OpenAI API Setup
Connect any custom agent to The Grid with the OpenAI API. Set the base URL, add a consumption key, choose an instrument, and handle 402 retries.
If you're building or running a custom agent harness, The Grid plugs in as a drop-in OpenAI-compatible backend. You don't need a tool-specific integration: any harness that issues HTTP calls or uses an OpenAI SDK can target The Grid by changing three values.
Need credentials programmatically? Follow Programmatic onboarding (OAuth device flow on the Platform API → POST /v1/api-keys) instead of copying a key from the dashboard.
For the full instrument catalog, see Current instruments. For framework-specific patterns (CrewAI, Deep Agents, DeerFlow, Hermes), see those integration pages.
Connection details
Base URL:
https://api.thegrid.ai/v1Auth header:
Authorization: Bearer <YOUR_CONSUMPTION_KEY>Format: OpenAI Chat Completions (drop-in for any OpenAI SDK)
Anthropic Messages beta:
https://messages-beta.api.thegrid.ai/v1withx-api-key: <YOUR_CONSUMPTION_KEY>Model parameter: an instrument string like
text-prime,code-prime, oragent-prime
Verify the key first
The inference request below requires sufficient account credits. Check your balance and add credits if needed before running it. See the Quickstart. To verify credentials without making a billable inference call, use Diagnostics.
curl https://api.thegrid.ai/v1/chat/completions \
-H "Authorization: Bearer $THEGRID_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"text-prime","messages":[{"role":"user","content":"ping"}]}'A 200 confirms the consumption key works. 401 means the key is wrong or it's a Trading key (you need a Consumption key for inference). 402 means credits are empty.
Drop-in client setup
Same SDK, same message format. Change base_url, api_key, and model. Keep everything else.
Resilient retry pattern
Long-running agents need to handle one edge case: if your harness goes idle and credits drop to zero, the first request after that returns 402. With Auto-Reload enabled, the platform tops up credits asynchronously; your retry succeeds within a second or two. Wrap your client call:
This pattern handles cold-start after idle periods. During continuous usage, Auto-Reload keeps credits topped up without interruption.
Routing across instruments
A single agent run typically benefits from routing tasks across tiers:
Planning, deep reasoning, or large context.
agent-max,text-max, orcode-max.General execution.
agent-prime,text-prime, orcode-prime. Most agent loops belong here.Tool calls, classification, routing, batch edits.
*-standard. Fast and cheap for high-volume work.
Pick the instrument per call rather than running everything on one tier. See Routing patterns for the full playbook.
What works out of the box
Every Grid instrument supports streaming, tool calling, JSON mode, and structured outputs in the OpenAI format. Use stream=True, tools=[...], and response_format=... exactly as you would against OpenAI directly.
Troubleshooting
401 Unauthorized. The key is wrong, or it's a Trading key (Consumption keys authenticate inference). Generate a Consumption key from your dashboard.402 Payment Required. Credits are empty. Top up or enable Auto-Reload at app.thegrid.ai. Auto-Reload pulls funds from your saved payment method when credits drop below the threshold you configure.404 Not Foundon a model. The model string didn't match a current instrument. Use one of the IDs from Current instruments.5xxfrom a supplier. The matching engine occasionally hits a transient supplier failure. Retry with the same backoff pattern as402.
Last updated
Was this helpful?