🖼️Framework Guide
Basic Usage
Hello World Example
pythonCopyfrom praxis_ai import Agent
agent = Agent(
'gemini-1.5-flash',
system_prompt='Be concise, reply with one sentence.',
)
result = agent.run_sync('Where does "hello world" come from?')
print(result.data)
Advanced Features
Tools & Dependency Injection
pythonCopyfrom dataclasses import dataclass
from praxis import BaseModel, Field
from praxis_ai import Agent, RunContext
@dataclass
class SupportDependencies:
customer_id: int
db: DatabaseConn
class SupportResult(BaseModel):
support_advice: str = Field(description='Advice returned to the customer')
block_card: bool = Field(description="Whether to block the customer's card")
risk: int = Field(description='Risk level of query', ge=0, le=10)
support_agent = Agent(
'openai:gpt-4',
deps_type=SupportDependencies,
result_type=SupportResult,
system_prompt=(
'You are a support agent in our bank, give the '
'customer support and judge the risk level of their query.'
),
)
System Prompts
pythonCopy@support_agent.system_prompt
async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str:
customer_name = await ctx.deps.db.customer_name(id=ctx.deps.customer_id)
return f"The customer's name is {customer_name!r}"
Tools
pythonCopy@support_agent.tool
async def customer_balance(
ctx: RunContext[SupportDependencies],
include_pending: bool
) -> float:
"""Returns the customer's current account balance."""
return await ctx.deps.db.customer_balance(
id=ctx.deps.customer_id,
include_pending=include_pending,
)
Monitoring and Debugging
Logfire Integration
pythonCopyimport logfire
logfire.configure()
logfire.instrument_asyncpg()
Best Practices
Code Organization
Keep agent definitions in separate modules
Use dependency injection for external services
Implement proper error handling
Document tools and dependencies
Performance Optimization
Use async/await for I/O operations
Implement proper caching strategies
Monitor and optimize token usage
Leverage streaming for large responses
Testing
Use dependency injection for mocking
Implement unit tests for tools
Create integration tests for full workflows
Use Logfire for debugging and monitoring
Last updated