Getting started¶
This page installs the package, composes a first agent with one tool and runs it, first offline with a scripted model, then on Claude.
Install¶
The Python implementation requires Python 3.12 or newer and depends only on httpx.
Until the first release, the package is installed from GitHub:
A first agent¶
The agent answers questions about the weather. It needs four things: a tool, a model, a harness and the agent that joins them.
The tool¶
A tool is a class with a definition, which the model sees, and an execute() method, which the loop calls with the input the model asked for. The result is returned as a ToolOutput, here built with the text_output() helper. Problems the model should know about, such as an unknown city, are returned as an error result instead of being raised.
FORECASTS = {"oslo": "snow, -3°C", "lisbon": "sunny, 19°C"}
class GetForecast(Tool):
readonly = concurrent = True # only reads, so several calls may run in parallel
@property
def definition(self):
return {
"name": "get_forecast",
"description": "Get today's weather forecast for a city.",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}
async def execute(self, input, ctx=None):
forecast = FORECASTS.get(input["city"].lower())
if forecast is None:
return text_output(text=f"no forecast for {input['city']}", is_error=True)
return text_output(text=forecast)
readonly and concurrent are hints for the rest of the system: this tool only reads, so permission rules may allow it freely and the loop may run several calls of it in parallel.
The model¶
PlaybookModel is a scripted model. It picks a playbook by a keyword in the task and plays one turn per model call: a list of tool calls (run in parallel) or an answer. It needs no network and no API key, which makes it the model of choice for tests and for this page.
model = PlaybookModel(ref="playbook:weather", playbooks={
"weather": [
[call("get_forecast", city="Oslo"), call("get_forecast", city="Lisbon")],
say(lambda results: f"Oslo: {results[0]}. Lisbon: {results[1]}."),
],
})
The harness and the agent¶
The harness holds everything around the model. The loop and the budget are required, so every agent has a guaranteed end. The tools, the event bus and the other parts are optional. TranscriptBus prints each tool call and result.
agent = ComposedAgent(
model=model,
harness=Harness(
loop=StandardLoop(),
budget=TreeBudget(limit={"steps": 5}),
tools=StaticToolSet(tools=[GetForecast()]),
events=TranscriptBus(label="weather_agent"),
),
initial_state=InitialState(system="Answer questions about the weather. Use get_forecast."),
)
The run¶
async with starts the model and the tools before the first run and closes them after the last. run() adds the task to the agent's conversation and runs the loop until it stops.
async def main():
async with agent:
outcome = await agent.run("What is the weather in Oslo and Lisbon?")
print(outcome.reason)
print(message_text(outcome.final))
asyncio.run(main())
Output:
[weather_agent] → get_forecast(city='Oslo')
[weather_agent] ← get_forecast: snow, -3°C
[weather_agent] → get_forecast(city='Lisbon')
[weather_agent] ← get_forecast: sunny, 19°C
[weather_agent] ■ done (end_turn)
end_turn
Oslo: snow, -3°C. Lisbon: sunny, 19°C.
The run took two model calls. In the first, the model asked for both forecasts at once, and the loop ran the two calls in parallel. In the second, the model answered, so the run ended with the stop reason end_turn.
The same agent on Claude¶
Replacing the model is a change to one line. AnthropicModel calls the Anthropic Messages API and reads the API key from the ANTHROPIC_API_KEY environment variable:
The tool, the harness and the agent stay exactly as they are. The complete file is docs/examples/first_agent_claude.py.
Cost tracking
AnthropicModel does not know the prices of the models, so the cost it reports is 0 and a budget limit on cost_usd never stops it. To track cost, build the model from an endpoint with prices, as shown on the Model page, or limit tokens instead.
Next steps¶
The Concepts pages explain every block used above and the ones not used yet: the context policy, the run store, custom event rules and composition. The research team tutorial composes two agents where one uses the other as a tool.
All code on this site is taken from files in docs/examples/, which the test suite runs, so every example is known to work.