Skip to content

Command

alloy.command

command

command(fn=None, *, output=_MISSING, tools=None, model=None, temperature=None, max_tokens=None, system=None, retry=None, retry_on=None)

Decorator to declare an AI-powered command.

The wrapped function returns an English prompt specification. This executes the model with optional tools and parses the result into the annotated return type. The retry parameter represents total attempts (minimum 1).

Usage

Text command

from alloy import command, configure

configure(model="gpt-5-mini", temperature=0.2)

@command  # returns str by default
def summarize(text: str) -> str:
    return f"Summarize in one sentence: {text}"

print(summarize("Alloy lets you write typed AI functions in Python."))

Typed output (dataclass)

from dataclasses import dataclass
from alloy import command

@dataclass
class ArticleSummary:
    title: str
    key_points: list[str]

@command(output=ArticleSummary)
def summarize_article(text: str) -> str:
    return f"Summarize with: title and 3–5 key_points. Article: {text}"

res = summarize_article("Python emphasizes readability and has a vast ecosystem.")
print(res.title)

See also

  • Guide → Commands: guide/commands.md
  • Guide → Structured Outputs: guide/structured-outputs.md
  • Guide → Tools & Workflows: guide/tools-and-workflows.md
  • Guide → Streaming: guide/streaming.md