Meta Model API cookbook

Ship on Muse Spark with recipes that run the first time you copy them. Each recipe solves one focused problem, shows working code, and points to what's next. Start with API fundamentals to nail the primitives, then layer on agent loops and full use cases.

API fundamentals

Validate one building block at a time and walk away with a starter you can extend.

Quickstart: chat completions Point the OpenAI SDK at a new base URL and make your first Muse Spark call.
Streaming responses Stream tokens as they generate and read usage in the final chunk.
Tool and function calling Detect a tool call, execute it, and feed the result back into the loop.
Structured output Return JSON that matches your schema and parses on the first try.
Prompt caching Reuse a stable prompt prefix and measure cached tokens.
Reasoning and thinking tokens Set reasoning effort and replay reasoning across turns.
Vision input Send images by URL or base64 and get structured analysis back.
Long context Pack repo-scale context into a single context window.
Error handling and retry Back off with jitter and skip retries on client errors.
Search grounding Ground answers in live web search with inline citations.

Agent patterns

Turn a model into an agent. These loops handle planning, self-correction, and staying coherent across long runs.

Basic agent loop Wire up the core perceive-decide-act loop.
Interleaved reasoning and tool use Interleave reasoning with tool calls in a single turn.
Multi-turn context management Keep context under control across a long agent run.
Search-and-replace edits Make exact-match file edits that stay reviewable.
Alert fatigue copilot Pull grounded patterns from noisy alerts, then probe, chat, and self-assess with strict JSON.

Use cases