
Ai
Install this when you are building Python AI agents or chat apps and need the Vercel Python `ai` SDK patterns for models, streaming, tools, and structured output.
Overview
ai is an agent skill for the Build phase that teaches solo builders how to use the Python `ai` SDK for models, streaming agents, tools, MCP, and structured output.
Install
npx skills add https://github.com/vercel-labs/py-ai --skill aiWhat is this skill?
- Covers models and providers via `ai.get_model`, AI Gateway, and optional OpenAI/Anthropic extras (`uv add "ai[openai]"`
- Documents async streaming with `ai.stream` and `agent.run` as context managers, iterating `TextDelta` and reading final
- Includes tools, agents, MCP, AI SDK UI, structured output, and media generation patterns in one Python package.
- Quick-start weather-assistant example with `@ai.tool`, system/user messages, and gateway model id `gateway:anthropic/cla
- Install path: `uv add ai` with `import ai` as the entry point.
Adoption & trust: 15 installs on skills.sh; 70 GitHub stars; 1/3 security scanners passed (skills.sh audits).
What problem does it solve?
You are coding a Python AI feature but lack a concise map of how the `ai` package wires models, async streams, tools, and providers together.
Who is it for?
Indie builders adding LLM agents, tool calling, or streaming chat to a Python API or automation service with uv.
Skip if: Teams that only need a no-code chat widget with zero custom Python, or projects standardized on a different stack (e.g. only raw OpenAI SDK with no Vercel `ai` package).
When should I use this skill?
Use when working with the Python `ai` SDK (install, models, streams, tools, agents).
What do I get? / Deliverables
After following the skill, you can install the SDK with uv, configure gateway or direct providers, and ship working agent streams with tools in your backend.
- Working agent or stream handler code
- Provider and optional extra configuration (`ai[openai]` / `ai[anthropic]`)
- Tool definitions wired into `ai.agent`
Recommended Skills
Journey fit
The skill is a hands-on SDK reference for implementing agent runtimes, streams, and tool calling in application code—core Build-phase engineering, not discovery or launch work. Content centers on `ai.agent`, tools, MCP, hooks, and event streams—the agent-tooling shelf where solo builders wire LLM behavior into backends.
How it compares
Use as a procedural SDK guide inside the repo—not a generic prompt pack and not an MCP server catalog entry.
Common Questions / FAQ
Who is ai for?
Solo and indie developers building Python backends or agents who want Vercel's `ai` SDK patterns for models, streams, tools, and MCP in one place.
When should I use ai?
During Build when you implement agent routes, streaming responses, or provider configuration; also when debugging agent streams or adding tools to an existing Python service.
Is ai safe to install?
Treat it like any third-party skill: review the Security Audits panel on this Prism page and inspect the SKILL.md source before letting an agent run install or network commands.
SKILL.md
READMESKILL.md - Ai
# ai Use this skill when working with the Python `ai` SDK. ```bash uv add ai ``` Direct OpenAI-compatible and Anthropic-compatible providers require optional extras: `uv add "ai[openai]"` or `uv add "ai[anthropic]"`. AI Gateway works with the base package. ```python import ai ``` ## Quick start ```python import asyncio import ai @ai.tool async def get_weather(city: str) -> str: """Get current weather for a city.""" return f"Sunny, 72F in {city}" async def main() -> None: model = ai.get_model("gateway:anthropic/claude-sonnet-4") agent = ai.agent(tools=[get_weather]) messages = [ ai.system_message("You are a helpful weather assistant."), ai.user_message("What's the weather in Tokyo?"), ] async with agent.run(model, messages) as stream: async for event in stream: if isinstance(event, ai.events.TextDelta): print(event.chunk, end="", flush=True) print(stream.output) if __name__ == "__main__": asyncio.run(main()) ``` `ai.stream(...)` and `agent.run(...)` are async context managers. Iterate events inside the context. After iteration, read final state from the stream object. ## Models and providers ```python model = ai.get_model() # reads AI_SDK_DEFAULT_MODEL model = ai.get_model("anthropic/claude-sonnet-4") # unprefixed: gateway route model = ai.get_model("gateway:anthropic/claude-sonnet-4") model = ai.get_model("openai:gpt-5.4") # direct provider route model = ai.get_model("anthropic:claude-sonnet-4-6") ``` - Gateway credentials use `AI_GATEWAY_API_KEY`. - Direct providers use provider-specific env vars such as `OPENAI_API_KEY` and `ANTHROPIC_API_KEY`. - Use `ai.get_provider(...)` when you need a custom base URL, API key, headers, or client. - Use `await ai.probe(model)` to check credentials and model availability. ```python provider = ai.get_provider( "openai", base_url="http://localhost:1234/v1", api_key="your_access_token_here", ) model = ai.Model("local-model", provider=provider) models = await ai.get_provider("anthropic").list_models() ``` Request-scoped provider options go through `params`: ```python params = { "providerOptions": { "gateway": {"sort": "cost"}, "anthropic": {"speed": "fast"}, } } async with ai.stream(model, messages, params=params) as stream: async for event in stream: ... ``` ## Messages and events Messages are Pydantic models with typed parts. Use builders for common roles and parts: ```python ai.system_message("Be concise.") ai.user_message("Describe this image:", ai.file_part(image_bytes, media_type="image/png")) ai.assistant_message(ai.thinking("scratchpad"), "Final answer") ai.tool_result_part("tc-1", result={"temp": 72}, tool_name="get_weather") ai.tool_message(tool_call_id="tc-1", result=72, tool_name="get_weather") ``` Common message properties: - `message.text`, `message.reasoning`. - `message.tool_calls`, `message.tool_results`. - `message.builtin_tool_calls`, `message.builtin_tool_returns`. - `message.files`, `message.images`, `message.videos`. - `message.get_output()` or `message.get_output(MyModel)`. Streams and agents yield event objects from `ai.events`: ```python async with ai.stream(model, messages, tools=tools) as stream: async for event in stream: if isinstance(event, ai.events.TextDelta): print(event.chunk, end="", flush=True) elif isinstance(event, ai.events.ToolEnd): print(event.tool_call.tool_name, event.tool_call.tool_args) elif isinstance(event, ai.events.ToolCallResult): for result in event.results: print(result.tool_name, result.result) elif isinstance(event, ai.events.HookEvent): print(event.hook.hook_id, event.hook.status) elif isinstance(event,