Basic Agent
import asyncio
from splinter import Gateway, AgentBuilder, LLMProvider
async def main():
# Create gateway
gateway = Gateway()
gateway.configure_provider("openai", api_key="sk-...")
# Build agent
agent = (
AgentBuilder("analyst")
.with_provider(LLMProvider.OPENAI, "gpt-4o-mini")
.with_system_prompt("Analyze data. Output JSON.")
.build(gateway)
)
# Run
result = await agent.run(task="Analyze sales trends")
print(result)
asyncio.run(main())
With Limits
from splinter import ExecutionLimits
gateway = Gateway(
limits=ExecutionLimits(
max_budget=1.0,
max_steps=10,
)
)
With State
from splinter import SharedState
state = SharedState()
state.set("context", "Q4 2024 sales data")
result = await agent.run(
task="Analyze the data",
state=state,
)
With Tools
agent = (
AgentBuilder("researcher")
.with_provider(LLMProvider.OPENAI, "gpt-4o-mini")
.with_system_prompt("Research the topic.")
.with_tools(["web_search", "read_file"])
.build(gateway)
)
Output Schema
agent = (
AgentBuilder("analyst")
.with_provider(LLMProvider.OPENAI, "gpt-4o-mini")
.with_system_prompt("Analyze data.")
.with_output_schema({
"type": "object",
"properties": {
"analysis": {"type": "string"},
"confidence": {"type": "number"},
},
"required": ["analysis"],
})
.build(gateway)
)
Error Handling
from splinter import BudgetExceededError, StepLimitExceededError
try:
result = await agent.run(task="...")
except BudgetExceededError:
print("Budget exceeded!")
except StepLimitExceededError:
print("Too many steps!")