AI
animica.ai.infer gives every function access to AI inference served by the Animica miner network. Calls are mediated by the host broker, metered per token, and billed into the same execution receipt as CPU and memory.
Using it
Declare the AI_INFERENCE capability on your function — without it, every call is refused with CapabilityDenied.
import animica
def main(request, ctx):
# simple prompt form
text = animica.ai.infer("Summarize: " + request["text"], max_tokens=200)
# chat form (OpenAI-style messages)
text = animica.ai.infer(
messages=[
{"role": "system", "content": "You are terse."},
{"role": "user", "content": request["question"]},
],
max_tokens=300,
temperature=0.2,
)
return {"answer": text}animica.ai.chat(messages, …)is an alias ofinfer(messages=…).- The last 20 messages are forwarded; each message's content is capped at 20,000 characters.
modelis optional — requests route to what the miner network is serving (the free, chain-served tier).
Budgets
| budget | default | error when exceeded |
|---|---|---|
| AI calls per execution | 8 | BudgetExceeded |
| AI tokens per execution | 8192 | BudgetExceeded |
| max_tokens per call | capped at 8192 | clamped, not an error |
| per-call wall clock | 60s | AnimicaError (ai_unavailable) |
What it costs
AI tokens are metered at aiTokenInNanm (1000 nANM/token default) and aiTokenOutNanm (3000 nANM/token default) from the live pricing policy — see Pricing & economics. Token counts come from the serving miner's usage report and appear in the execution receipt (usage.aiTokensIn/aiTokensOut).
Design for degraded serving
Inference is served by a decentralized miner fleet: availability is real-world, not guaranteed. When no healthy provider serves a request, ai.infer raises animica.AnimicaError after its 60s window. Production functions should decide what happens next — fail the request, retry later via a schedule, or degrade to a deterministic computation and say so:
try:
summary = animica.ai.infer(prompt, max_tokens=220)
engine = "animica-ai"
except animica.BudgetExceeded:
# this execution's AI call/token budget is used up
raise
except animica.AnimicaError:
# no healthy provider served the request — degrade honestly
summary = extractive_summary(text) # real, deterministic, in-sandbox
engine = "extractive-fallback"AnimicaError and degrading keeps your endpoint useful — and your response should always tell the caller which engine produced the result, as both AI examples do.Serving AI to the network
The miners answering these calls earn ANM for it. Any capable machine can join: pip install -U animica && animica up qualifies the hardware and serves the models it can actually run. That supply side is what keeps in-function AI available — see Compute providers for the Python Cloud execution fleet, which works the same way.