ExamplescriptintermediateRunnableguided-flow
Agents
Runnable example (intermediate) for script.
Key Facts
- Level
- intermediate
- Runtime
- Python
- Pattern
- Inspectable flow with visible system boundaries
- Interaction
- Live sandbox • Script
- Updated
- 14 March 2026
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Linked principles
1-agents.py
python
from datetime import date
import json
import nest_asyncio
from pydantic_ai import Agent
nest_asyncio.apply()
# --------------------------------------------------------------
# Static instructions - defined at agent creation
# --------------------------------------------------------------
agent = Agent(
"openai:gpt-4o-mini",
instructions="You are a helpful assistant. Be concise.",
)
result = agent.run_sync(user_prompt="What is Python?")
print(result.output)
# --------------------------------------------------------------
# Dynamic instructions - computed at runtime
# --------------------------------------------------------------
agent = Agent("openai:gpt-4o-mini")
@agent.instructions
def add_date() -> str:
return f"Today's date is {date.today()}."
@agent.instructions
def add_context() -> str:
return "Use a friendly, conversational tone."
result = agent.run_sync(user_prompt="What day is it?")
print(result.output)
# --------------------------------------------------------------
# Combining static and dynamic instructions
# --------------------------------------------------------------
agent = Agent(
"openai:gpt-4o-mini",
instructions="You are a helpful assistant.",
)
@agent.instructions
def add_timestamp() -> str:
return f"Current date: {date.today()}"
result = agent.run_sync(user_prompt="What year are we in?")
print(result.output)
# --------------------------------------------------------------
# Let's explore the result
# --------------------------------------------------------------
print(result.all_messages())
messages = json.loads(result.all_messages_json())
print(json.dumps(messages, indent=2))
Related principles
- P4trustApply progressive disclosure to system agencyProvide the minimum information necessary by default, while enabling users to inspect additional detail when confidence, understanding, or intervention is required.Open principle →
- P7trustEstablish trust through inspectabilityUsers should be able to examine how a result was produced when confidence, accountability, or decision quality is important.Open principle →
- P8trustMake hand-offs, approvals, and blockers explicitWhen the system cannot proceed, the reason should be immediately visible, along with any action required from the user or another dependency.Open principle →
- P9orchestrationRepresent delegated work as a system, not merely as a conversationWhere work involves multiple steps, agents, dependencies, or concurrent activities, it should be represented as a structured system rather than solely as a message stream.Open principle →