Configure LiteLLM RoutingΒΆ

autoform uses the active LM client at execution time. By default that client calls LiteLLM directly. Use lm_client for a configured litellm.Router with retries, aliases, or provider fallback.

from litellm import Router
import autoform as af


model_list = [dict(model_name="docs-model", litellm_params=dict(model="gpt-5.5"))]
router = Router(model_list=model_list, num_retries=2)


def explain(topic: str) -> str:
    prompt = af.format("Explain {} in one paragraph.", topic)
    msg = dict(role="user", content=prompt)
    # docs-model is resolved by the active router
    return af.lm_call([msg], model="docs-model")


ir = af.trace(explain)("recursion")

# credentials are still provider credentials, such as openai_api_key or env vars
with af.lm_client(router):
    print(ir.call("recursion"))

The context applies when the IR executes, not when it is traced. That means the same IR can run with different routers:

# run the same ir with a different execution context
with af.lm_client(router):
    answer = ir.call("memoization")

print(answer)

Keep provider-specific routing policy in LiteLLM. Keep program structure in autoform: trace the Python function, transform the IR, and choose the LM client around execution.