Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,15 +575,65 @@ the start of the entry's title.

### Added

- **`AgentConfig.model_choices`: the models a delegation may run a type on
instead of its own, on both backends.** References, bare model ids or
roles (`model_choices=("fast",)`; the agent YAML's `model_choices:
[fast]`; empty by default), each resolved with the type's
`model_settings` when the manager initializes, all or nothing with the
agents' models (`validate_settings` parses them and checks their
providers' keys first; a built-in role no setting maps, such as `fast`,
is computed from the model list, its provider listed at startup), and
given its own context numbers: a window too small to work in fails
startup (`ContextBudgetError`), as a type's own model does. Each choice
is its type built once more at initialization, on that model: on ADK an
agent and a runner of its own, whose plugin chain reads the choice's
model, provider kind and context numbers under the type's name, so the
fit guard and the overflow handler keep a run within them; on Deep
Agents a graph of its own, summarized by the choice's numbers. A model
switch builds them again, and the release closes them with the type's. A
run on a choice keeps its type's name in its events and records; its
usage is the choice's model's (`RunRecord.usage`, the ledger's
`by_model`, the raw LLM log). The manager gives each choice's model and
numbers: `choice_models[(agent, choice)]` and `context_budget(agent,
model=choice)`. `validate_agent_graph` raises `AgentGraphError` for
`model_choices` on the root, for one that is no tuple or list of texts,
and for a choice named twice.
- **`task`'s `tools` and `model`: a delegation may narrow its run's tools and
pick a cheaper model, never widen, on both backends.** Every `task` call
may give `tools`, some of the type's own tools (its registered, skill and
MCP tools, by the names its model is offered them; the plan's tools and
the delegation tools are always kept): the run's model is offered only
those, and a call of any other is answered as one of a tool the agent
does not have ("<tool> failed: ValueError: Tool '<tool>' not found.", then
the tools it has), and the run goes on. A call to a type with model
choices may give `model`, one of them: the run is on that model. Anything
else is the call's error result, and no run starts nor counts toward the
turn's ("The tools for 'researcher' must be some of its own: lookup.",
"The model for 'researcher' must be one of its model choices: fast.",
"'researcher' has no model choices; leave model out."; a backend that
cannot list the type's MCP tools just now says so, "The tools of
'researcher' could not be listed just now (OSError: …); try again.").
`task`'s schema always has `tools` (with the default policy it is otherwise as
it was), and `model` when a type it offers has choices, which its line names
(`- researcher (model choices: fast): …`). The instance keeps them
(`InstanceRecord.tools`, `.model`, in the records beside the
conversation; records written before them read as all the tools and the
type's own model): its follow-ups, the user's and after a restart, run
with them, and one whose type no longer has them is refused
("researcher-1 was started with the model fast, which 'researcher' no
longer has; start a new one with task."). `/agents <id>` shows `Model`
and `Tools`, and the instance's view its model and its context against
the choice's numbers (the status bar's `ctx`, `/status`).
- **Agent types started at run time: `AgentConfig.spawnable_by`, free types
and `SubAgentPolicy.spawn_sources`, on both backends.** A type may be
started while the app runs, besides by its parent in the configured tree,
when the app allows it twice: the type's `spawnable_by` (a set of `"agent"`,
`"skill"`, `"user"`; the agent YAML's `spawnable_by: [agent]`; None, the
default, for a type only its parent starts) and the policy's
`spawn_sources` (the same three, `frozenset()` by default: nothing is
started at run time, and an app keeps its `task` as it was, byte for
byte). An agent's `task` call is the `"agent"` source: `task` lists, after
started at run time, and an app keeps its `task` as it was, but for the
`tools` argument every call has: see **`task`'s `tools` and `model`…**).
An agent's `task` call is the `"agent"` source: `task` lists, after
the caller's own sub-agents, the types agents may start, each with its
description (`agentic_cli.workflow.agents.spawn`, provisional: `spawnable`,
`may_spawn`, `can_have_children`; the manager's delegations'
Expand Down Expand Up @@ -1953,7 +2003,9 @@ the start of the entry's title.
may start (its own sub-agents first),
each listed in the tool's description with what it does and the brief
fields it requires, then the brief, `title` and `objective` (required),
`deliverable`, `constraints`, `known` and `done_when`. The run's first
`deliverable`, `constraints`, `known` and `done_when`, then `tools` (and, for
a type with model choices, `model`: see Added: **`task`'s `tools` and
`model`…**). The run's first
message is the task message, `# Task: <title>` and a section per filled
field (`## Objective`, `## Deliverable`, `## Constraints`, `## What is
already known`, `## Done when`). A call whose brief misses a field its
Expand Down
30 changes: 15 additions & 15 deletions CLAUDE.md

Large diffs are not rendered by default.

27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,7 @@ configs = [coordinator, researcher, analyst]
| `limits` | AgentLimits \| None | The agent's own per-run limits (`max_model_calls`, `timeout` in seconds, `max_tokens`), laid over the app's `SubAgentPolicy.limits` field by field: each of its runs works under them, and one past a limit ends (see [Sub-agent context](#sub-agent-context)). Not on the root |
| `continuable` | bool | Whether an instance of this agent takes follow-ups after its run (`True`, the default) or is closed when its run ends (`False`, one-shot); see [Sub-agent context](#sub-agent-context). Not `False` on the root |
| `spawnable_by` | frozenset[str] \| None | Who may start this agent at run time besides its parent: a set of `"agent"` (another agent's `task` call), `"skill"` and `"user"`, each also enabled by the app's `SubAgentPolicy.spawn_sources`; `None` (the default): only its parent. One in no `sub_agents` list is a free type, never the root; see [Sub-agent context](#sub-agent-context). Not on the root |
| `model_choices` | tuple[str, ...] | The models a delegation may run this agent on instead of its own (`task`'s `model`): references, bare ids or roles, as `model` takes them (`("fast",)`); each is resolved, with the agent's `model_settings`, and given its own context numbers at startup. Empty (the default): its own model alone; see [Sub-agent context](#sub-agent-context). Not on the root |

The agent graph is validated before anything is allocated — ahead of model
discovery and service creation. Duplicate names, unknown `sub_agents`
Expand All @@ -1304,6 +1305,8 @@ run time only, and no root however it is listed; the root may not set
`spawnable_by`, and a `spawnable_by` that is empty or names a source other
than `agent`, `skill` and `user` raises too (a free type the app's policy lets
no source start is a warning: `validate_agent_graph(configs, policy=...)`).
`model_choices` on the root, one that is no tuple or list of texts, and a choice
named twice raise it too.
`inherit_context=True` on the root, on an agent with sub-agents of its own or
on an agent below another sub-agent, and an `inherit_context` that is not True
or False, raise it too, and, when the agents are built, so does a coordinator
Expand All @@ -1324,7 +1327,8 @@ both backends. The parent calls the framework's `task` tool, the same on both:
`agent_type`, one of its own sub-agents (or of the types it may start at run
time, below), and a brief, `title` and `objective`
(required), `deliverable`, `constraints`, `known` (what the parent already
knows that the task needs) and `done_when`. The sub-agent's run starts from its
knows that the task needs) and `done_when`, and optionally `tools` and `model`
(below). The sub-agent's run starts from its
task message, the brief as sections (`# Task: <title>`, `## Objective`, …); it
has a plan and a task list of its own (it starts with none, and what it saves
stays with it: `/agents <id>` shows them), and works with its own prompt,
Expand Down Expand Up @@ -1453,6 +1457,26 @@ reviewer = AgentConfig(name="reviewer", prompt="Check what was said.",
subagents=SubAgentPolicy(spawn_sources={"agent"}),
)
```
- **Narrowing a run, and a cheaper model.** Any `task` call may give
`tools`, some of the type's own tools (its registered, skill and MCP
tools, by name; its plan's tools and the delegation tools always stay):
the run is offered only those, and a call of another is answered as one
of a tool it does not have. A type's `model_choices` are the models a call
may pick instead of its own (`model`, named in the type's line in `task`'s
description); each is resolved and given its context numbers at startup,
and runs as the type's agent built on it (ADK: a runner of its own;
Deep Agents: a graph of its own), within its own window. Anything else is
the call's error result, naming what it may be, and no run starts. The
instance keeps both: its follow-ups, and its runs after a restart, run
with them, and `/agents <id>` shows them.

```python
researcher = AgentConfig(name="researcher", prompt="Find facts.",
description="Finds facts.", tools=[web_search, web_fetch],
model="strong", model_choices=("fast",))
# The coordinator's call: {"agent_type": "researcher", "title": "Look it up",
# "objective": "...", "tools": ["web_search"], "model": "fast"}
```
- **The parent's tools.** With `subagents=SubAgentPolicy(follow_ups=True)`
every agent that can have sub-agents (its own, or types it may start at run
time) gets three more tools beside `task` (with the
Expand Down Expand Up @@ -1648,6 +1672,7 @@ are `AgentConfig`'s, and an unknown one is an error:
| `sub_agents`, `inherit_context` | The agents it may delegate to, and what a sub-agent sees: `false` (its task alone, the default) or `true` (its parent's conversation too; see [Sub-agent context](#sub-agent-context)) |
| `brief_required`, `limits`, `continuable` | As on `AgentConfig`: `brief_required: [objective, deliverable]`, `limits: {max_model_calls: 30, timeout: 600, max_tokens: 200000}` (each limit read as written), `continuable: false` (one-shot) |
| `spawnable_by` | Who may start it at run time, a list: `spawnable_by: [agent]` (read as a set; with no `sub_agents` naming it, a free type) |
| `model_choices` | The models a delegation may run it on instead of its own, a list: `model_choices: [fast]` (read as a tuple) |
| `include_state_tools` | `true` by default: the plan and task tools are added |

- **A tool is a name, resolved when the manager is built.** A bare name is
Expand Down
31 changes: 23 additions & 8 deletions src/agentic_cli/cli/agent_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
recap of it: its brief's title and objective, its last run's status, the
first lines of its result and its tokens; nothing is replayed. The prompt
becomes ``researcher-2> `` and the status bar names it and its status, its
context (``ctx``: its own last prompt against its own summary trigger) and
context (``ctx``: its own last prompt against its own summary trigger, the
numbers of its model choice when it runs on one) and
its tokens, the jobs segment kept.

A line typed there is the user's message to the instance: while it is idle
Expand Down Expand Up @@ -170,10 +171,10 @@ def context_rows(self) -> list[tuple[str, str]]:
if record is None:
return [("Sub-agent", f"{self.instance} (gone)")]
rows = [("Sub-agent", f"{self.instance} ({record.agent}, {record.status})")]
model = getattr(workflow, "agent_models", {}).get(record.agent)
model = _model(workflow, record)
if model is not None:
rows.append(("Model", str(model.ref)))
budget = _budget(workflow, record.agent)
budget = _budget(workflow, record)
if budget is not None:
window = (
format_tokens(budget.window) if budget.window_known
Expand Down Expand Up @@ -496,10 +497,23 @@ def _runs_of(workflow: Any, conversation: "SessionRef", instance: str) -> list["
]


def _budget(workflow: Any, agent: str) -> Any:
"""``agent``'s context budget; None when its manager manages none."""
def _model(workflow: Any, record: "InstanceRecord") -> Any:
"""The model the instance runs on: its model choice's when it has one, else its
type's."""
if record.model is not None:
return getattr(workflow, "choice_models", {}).get((record.agent, record.model))
return getattr(workflow, "agent_models", {}).get(record.agent)


def _budget(workflow: Any, record: "InstanceRecord") -> Any:
"""The instance's context budget (its model choice's, when it has one).

None when its manager manages none.
"""
try:
return workflow.context_budget(agent)
if record.model is not None:
return workflow.context_budget(record.agent, model=record.model)
return workflow.context_budget(record.agent)
except (KeyError, RuntimeError, AttributeError):
return None

Expand All @@ -518,8 +532,9 @@ def _last_prompt(workflow: Any, conversation: "SessionRef", record: "InstanceRec

def _context(workflow: Any, conversation: "SessionRef", record: "InstanceRecord") -> str:
"""The status bar's ``ctx`` for the instance: its last prompt against its
own trigger, as the main agent's is shown (``UsageTracker.format_context``)."""
budget = _budget(workflow, record.agent)
own trigger (its model choice's, when it has one), as the main agent's is
shown (``UsageTracker.format_context``)."""
budget = _budget(workflow, record)
return UsageTracker(
last_prompt_tokens=_last_prompt(workflow, conversation, record),
context_trigger=budget.trigger if budget is not None else None,
Expand Down
31 changes: 25 additions & 6 deletions src/agentic_cli/cli/agents_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
deeper. Enter opens the view of the one picked (``/agent <id>``), Esc
nothing; ``/agents <id>`` shows one instance
(``researcher-2``) or one run (``researcher-2#1``): its type, its parent, its
title, then each run's status (while it works ``running``, ``queued`` waiting
for a free working slot, or ``needs you`` waiting on the user; a completion
cut at its output limit ``completed (partial)``) and how long it ran (or has
title, the model choice it runs on and the tools its delegation narrowed it
to (when it was given any), then each run's status (while it works ``running``,
``queued`` waiting for a free working slot, or ``needs you`` waiting on the user; a
completion cut at its output limit ``completed (partial)``) and how long it ran (or has
been running), the fields of its brief its parent filled (its objective,
deliverable, constraints, what was known, when it is done; a follow-up's
message in their place), the first lines of its result, the plan and the
Expand Down Expand Up @@ -60,6 +61,8 @@

#: How many lines of a run's result ``/agents <id>`` shows.
RESULT_LINES = 3
#: What ``/agents <id>`` says of an instance narrowed to none of its type's own tools.
NO_OWN_TOOLS = "none of its own"
#: What ``/agents`` says for a conversation with no run recorded.
NO_RUNS = "No sub-agent runs in this conversation yet."
#: The title of ``/agents``'s picker.
Expand Down Expand Up @@ -199,7 +202,10 @@ def show(app: Any, workflow: Any, conversation: "SessionRef", wanted: str) -> No
mine = [found] if found.ref.run == wanted else [
r for r in runs.records(conversation) if r.ref.instance == found.ref.instance
]
app.session.add_rich(_one(mine, workflow.usage_of(conversation)))
app.session.add_rich(_one(
mine, workflow.usage_of(conversation),
instance=runs.instance(conversation, found.ref.instance),
))


async def _load(app: Any, workflow: Any, conversation: "SessionRef") -> bool:
Expand Down Expand Up @@ -248,15 +254,28 @@ def _workflow(app: Any) -> Any:
return workflow


def _one(records: list["RunRecord"], ledger: "UsageLedger") -> Panel:
"""``/agents <id>``'s panel: one instance's runs (one run's, for a run id)."""
def _one(
records: list["RunRecord"],
ledger: "UsageLedger",
*,
instance: "InstanceRecord | None" = None,
) -> Panel:
"""``/agents <id>``'s panel: one instance's runs (one run's, for a run id).

After its title it names the model choice and the tools of ``instance``
(its record) when its delegation gave it any.
"""
first = records[0]
table = Table(show_header=False, box=None, padding=(0, 2, 0, 0))
table.add_column("Key", style="bold cyan", no_wrap=True)
table.add_column("Value")
table.add_row("Type", Text(first.ref.agent))
table.add_row("Parent", Text(first.ref.parent_run or "root"))
table.add_row("Title", Text(first.title or "—"))
if instance is not None and instance.model is not None:
table.add_row("Model", Text(instance.model))
if instance is not None and instance.tools is not None:
table.add_row("Tools", Text(", ".join(instance.tools) or NO_OWN_TOOLS))
now = _now()
for record in records:
table.add_row(f"Run {_run_number(record)}", f"{_status(record)} · {_duration(record, now)}")
Expand Down
Loading
Loading