opendunes
Best practices · 6 min
Copy page ▾

Best practices

Habits that keep an app on the API cheap, steady and safe.

  1. Keep your keys safe.
  2. Pin the model.
  3. Bound each request.
  4. Plan for failure.
  5. Do repeated and patient work once.
  6. Watch what you spend.
  7. Keep the wait short and the thinking in room.

1. Keep your keys safe

DoWhy
Read the key from the environment: OPENDUNES_API_KEYa key written in the code ends up in the repository, and from there in every copy of it
Keep the key on your server, never in a browser page or a mobile appanyone who opens the page reads the key and spends your balanceBalanceWhat you can spend, in DA.More on Balance and charges →
Make one key for each app, up to 10 on an accounta key you pause or delete stops one app, and the others keep running
Give each key a cap in DADAThe Algerian dinar, the one currency the docs show.More on Balance and charges → and a reset: daily, weekly or monthlya loop that runs away stops at the cap, with a 402 on that key only: Limits
Tag each key Production or Testthe test traffic and the real traffic stay apart in your usage
Allow each key only the models its app callsa key that cannot call a model cannot be made to spend on it
Keep the management keyManagement keyThe key that runs your account through the API: the balance, the usage history, your keys, guardrails, observability. It never calls a model.More on API keys → out of the appsit runs your account and never calls a model: Your account through a management key
Don't

Don't share one key between your shop and your tests. A test that loops spends the cap the shop needs.

2. Pin the model

DoWhy
Send a full model IDModel IDThe name a request calls a model by, lab/model, copied from Models.More on Models →, such as anthropic/claude-sonnet-4.5, in productionthe same ID answers the same way tomorrow
Use a -latest alias in a script you watch, not in an app you do notthe alias moves to a newer model without a change in your code: Latest model and suffixes
Copy the ID from the cataloguea typed ID that is nearly right is a 404

3. Bound each request

DoWhy
Set the answer's limit yourself, max_tokensa request that leaves it open is given a limit your wallet can pay, which may be shorter than you want; one you set is never raised
Stream a long answer: Streamingthe reader sees the first words at once, and the connection does not sit idle
Ask for the shape you need: Structured outputsa reply in your shape needs no guessing in your code
Send only the context the answer needsevery token sent is read, and charged

4. Plan for failure

DoWhy
Name fallbacks: Fallbackswhen a model or its provider cannot answer, the next one does, and the user sees an answer
Retry only a 408, 429, 500, 502, 503 or 504, and a 402 that carries Retry-Afterthe same request can pass on a second try; a 400 or a 401 never will
Wait the seconds in Retry-After when the answer sends it, and wait longer each time when it does nota retry sent at once meets the same limit
Stop at a few triesa request that fails five times has a cause that waiting will not fix
Read error.message before you retrythe body says why, and a 402 names the limit that stopped it

What to do on every status: Errors and retries.

Watch out

A retry of a request that already ran is a second request, and it is charged. Retry what failed, not what answered.

5. Do repeated and patient work once

DoWhy
Cache an identical request: Response cachea copy answers at no charge, with no model running
Mark a long prompt that repeats, to pay the cache-read price for its start: Cachingthe part that does not change is read once
Send work that can wait as a batch: Batchesthe answer comes later, at the model's batch price
Don't

Don't cache an answer that must read differently each time. A copy comes back word for word.

6. Watch what you spend

DoWhy
Log usage.cost from every answerit is what that request cost, in DA
Read X-Balance-Available on an answer that is not streamedit is the balance left after the charge
Read the balance from a script that holds the management key: Your account through a management keya streamed answer carries no balance header, and one script can warn you before the wallet is empty
Log model from every answera router names the model it picked, and that is the one that was charged
DepositDepositMoney entering your balance, by CIB or Edahabia card.More on Balance and charges → before the balance runs out: Balance and chargesa request the wallet cannot pay for is a 402

7. Keep the wait short and the thinking in room

DoWhy
Name a low reasoning.effort for simple work, and a high one only where the answer needs ita model that reasons writes its thinking before its answer, so a lower effort ends sooner and costs less: Reasoning
Raise max_tokens when you raise the efforta thinking effort is a share of the answer limit: the thinking and the answer fit inside max_tokens together, so a higher effort under a small limit leaves a shorter answer
Set max_tokens to what the answer needs, not to the model's longesta request that names a limit is held whole: the longest answer it may take is paid for before it starts, so a limit far above the need can get a 402, or wait for money your running requests hold, when a smaller one would have run
Leave the effort out when you would rather have an answer than a refusalwhen the balance is short, an effort you left out drops so that an answer still comes; an effort or a thinking budget you named that the balance cannot pay is refused with a 402
Stream the answer: Streamingthe reader sees the first words while the rest is written
Send only the context the answer needsa shorter input is read sooner, and charged less
Watch out

A limit you name is never lowered, and a limit above the model's longest answer becomes the model's longest. What the request holds, and what comes back unused: How a request is charged.