Prerequisites
- Python 3.8 or higher
- The SDK installed:
pip install 0g-inference-sdk - A wallet with 0G tokens (get testnet tokens)
requestsandpython-dotenvinstalled
If you haven’t set up the SDK yet, follow the Installation guide first.
Supported service types
| Service type | Description | Billing unit |
|---|---|---|
chatbot | Conversational AI (LLMs) with OpenAI-compatible endpoints | Per token (input + output) |
text-to-image | Generate images from text prompts | Per image |
image-editing | Modify existing images | Per image |
speech-to-text | Transcribe audio to text | Per output token |
TeeML for TEE-verified providers).
List available services
Before making requests, discover what’s live on the network:ServiceMetadata with the fields:
provider— provider wallet address (unique identifier)service_type— one ofchatbot,text-to-image,image-editing,speech-to-texturl— provider endpoint base URLinput_price,output_price— prices in wei per unitmodel— model identifier (e.g.qwen/qwen-2.5-7b-instruct)verifiability—TeeMLif TEE-verified, empty otherwise
list_service() is paginated. Use offset and limit to page through large result sets, or pass include_unacknowledged=False to only see services whose TEE signer has been acknowledged on-chain.
Make your first inference request
The end-to-end flow is: fund your ledger, acknowledge the provider, transfer a sub-account balance, get auth headers, send the HTTP request.Fund your ledger
Create a prepaid ledger account with at least 3 OG (the contract minimum). This is a one-time setup — you can top up later with
deposit_fund.Acknowledge the provider
A one-time on-chain action that records the provider’s TEE signer to your account. Required before sending requests.This call is idempotent — if you’ve already acknowledged the provider, it returns immediately with
{"status": "already_acknowledged"}.Transfer funds to the provider sub-account
Each provider has its own sub-account under your main ledger. Transfer a small balance so the provider will accept your requests. The broker recommends a minimum of 1 OG per provider.
Sub-account transfers are fast but settle on-chain. Budget a few seconds for confirmation.
Generate authenticated headers
The SDK creates a single-use session token, signs it with your wallet, and returns the HTTP headers the provider expects.Session tokens are cached for 24 hours. You can also issue persistent API keys — see API Keys & Session Tokens.
Complete example
Verify TEE-signed responses
For providers withverifiability == "TeeML", you can cryptographically verify that the response was produced inside a Trusted Execution Environment.
verifiability == ""), process_response always returns True. See Response Verification for the full attestation flow, including verify_service() which checks the provider’s TEE quote against the on-chain signer.
How billing works
- Prepaid ledger — you deposit funds once; the SDK reads your balance on-chain.
- Provider sub-accounts — each provider locks a portion of your ledger. You must transfer funds to a provider before making requests.
- Delayed settlement — usage is deducted asynchronously after the request completes. Your sub-account balance may briefly show a higher value than you’ve actually spent.
- Refunds —
broker.ledger.retrieve_fund("inference")reclaims unused balances from all inference providers back to your main ledger.
Refunds are subject to the contract’s lock time (typically 24 hours) to protect providers from immediate withdrawal after a request.
Auto-funding
For long-running services, the SDK can top up a provider sub-account automatically when its balance falls below a threshold:Troubleshooting
403 Forbidden from provider
403 Forbidden from provider
The provider endpoint must include
/v1/proxy. Always use broker.inference.get_service_metadata(provider)["endpoint"] — it adds the path automatically.401 Unauthorized
401 Unauthorized
InsufficientBalanceError or 'account balance too low'
InsufficientBalanceError or 'account balance too low'
Either your main ledger is empty or the provider sub-account hasn’t been funded. Run:
ProviderNotAcknowledgedError
ProviderNotAcknowledgedError
Call
broker.inference.acknowledge_provider_signer(provider) before sending requests. This is a one-time action per provider.ServiceNotFoundError
ServiceNotFoundError
The provider address isn’t registered on the current network. Check you’re on the right network (testnet vs mainnet) and that the address appears in
list_service().Next steps
Account Management
Deep dive on deposits, transfers, refunds, and auto-funding.
API Keys & Session Tokens
Issue persistent API keys for server applications.
Response Verification
Verify TEE attestations and signed responses on-chain.
Using with OpenAI SDK
Point the official OpenAI Python SDK at a 0G provider.