Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/swe-agent/mini-swe-agent/llms.txt

Use this file to discover all available pages before exploring further.

mini-swe-agent is not just a CLI tool — it is a fully importable Python library. You can instantiate an agent, run it on any task string, and inspect or persist the resulting trajectory, all from your own Python code. This makes it straightforward to embed mini-swe-agent inside larger pipelines, evaluation harnesses, or custom run scripts.
Python bindings are included in the standard pip install mini-swe-agent install. No separate setup is required. For contributing or modifying source, follow the dev install in the quickstart.

Hello world

The minimal example creates a DefaultAgent with a model and a local environment, then calls agent.run(task).
import logging

from minisweagent.agents.default import DefaultAgent
from minisweagent.models import get_model
from minisweagent.environments.local import LocalEnvironment

logging.basicConfig(level=logging.DEBUG)

task = "Write a hello world program"
model_name = "anthropic/claude-sonnet-4-5-20250929"

agent = DefaultAgent(
    get_model(input_model_name=model_name),
    LocalEnvironment(),
)

result = agent.run(task)

Choosing a model

You can select a model with automatic class resolution via get_model, or instantiate a model class directly.
from minisweagent.agents.default import DefaultAgent
from minisweagent.models import get_model
from minisweagent.environments.local import LocalEnvironment

# get_model resolves the best model class automatically.
# For Anthropic models it also enables prompt caching by default.
agent = DefaultAgent(
    get_model(input_model_name="anthropic/claude-sonnet-4-5-20250929"),
    LocalEnvironment(),
)
result = agent.run(task)

Return value of agent.run()

agent.run() returns the extra dict from the final exit message. It always contains at least two keys:
KeyTypeDescription
exit_statusstrWhy the agent stopped ("Submitted", "LimitsExceeded", etc.)
submissionstrThe agent’s final answer or output
result = agent.run(task)

print(result["exit_status"])   # e.g. "Submitted"
print(result["submission"])    # the agent's final output

Accessing the full trajectory

After agent.run() returns, agent.messages contains the complete message history — every system prompt, user message, assistant response, and observation, in order.
for message in agent.messages:
    print(message["role"], message.get("content", "")[:80])

Saving the trajectory to disk

agent.save(path) serializes the full trajectory (messages, config, cost stats, version) to a JSON file. It also returns the serialized data as a dict.
from pathlib import Path

data = agent.save(Path("trajectory.json"))
# trajectory.json is now written; `data` is the same dict
You can also serialize without writing a file:
data = agent.serialize()

Cost and call tracking

DefaultAgent tracks cumulative cost and the number of model calls on the instance:
result = agent.run(task)

print(f"Total cost:  ${agent.cost:.4f}")
print(f"Total calls: {agent.n_calls}")
These attributes are updated after every model call and are safe to read at any point during or after a run.

Choosing an environment

Executes bash commands directly on the host machine using subprocess.run. Suitable for development and trusted workloads.
from minisweagent.environments.local import LocalEnvironment

agent = DefaultAgent(
    get_model(input_model_name=model_name),
    LocalEnvironment(),
)
You can pass cwd, timeout, or custom env vars:
LocalEnvironment(cwd="/tmp/workdir", timeout=60)

Build docs developers (and LLMs) love