Skip to content

Invoke-Agent

Terminal window
Invoke-Agent "why does the parser drop trailing newlines?"

Aliases: agent, pia.

On a terminal it opens the transcript viewer; redirected or under -NoUi it writes PsAgent.AgentEvent objects to the pipeline. With no prompt at all, the viewer opens empty and waits for i.

ParameterTypeDefaultNotes
PromptstringPosition 0, accepts pipeline input. What to ask.
WorkspaceRootstringcurrent locationAliases Root, Path. The directory the agent may read, write and run commands in.
Modelstringclaude-opus-5Model id.
MaxTokensint16000ValidateRange(256, 128000). Per-response output ceiling; 16000 is chosen to stay inside the SDK’s non-streaming HTTP timeout.
EffortLevelstringhighValidateSet("low", "medium", "high", "max"), case-insensitive. Reasoning effort; lower is cheaper and terser.
MaxTurnsint40ValidateRange(1, 500). Hard ceiling on model round-trips in one turn.
CommandTimeoutSecondsint120ValidateRange(1, 3600). Seconds a single run_bash call may take before its process tree is killed.
AutoApproveswitchoffRun edits and commands without asking.
NoUiswitchoffSkip the viewer and write transcript objects to the pipeline.
NoThinkingswitchoffDo not show the model’s reasoning rows.
Cssstringbuilt-in agent sheetAliases Style, Stylesheet. Stylesheet name, inline CSS, or a .pcss path.
SystemPromptstringbuilt-inReplace the built-in system prompt.
ApiKeystringresolvedThe key to authenticate with. See Authentication.
BaseUrlstringAnthropicAlias Endpoint. A gateway serving the Anthropic Messages API, without a version segment.
NoCredentialDiscoveryswitchoffDo not read credentials from CLI stores or .env.local.
OAuthProviderstringautoAlias OAuthProfile. Use a browser sign-in stored by Connect-Agent.
ApistringautoValidateSet("auto", "anthropic", "openai"). Which wire format the endpoint speaks.

A typical invocation:

Terminal window
Invoke-Agent "..." `
-WorkspaceRoot ./src `
-Model claude-opus-5 `
-EffortLevel medium `
-MaxTurns 40 `
-CommandTimeoutSeconds 120 `
-AutoApprove `
-NoThinking `
-NoUi

Each turn appends the prompt to the message list and then, up to MaxTurns times: create one Messages.Create request (system prompt + static tool catalog + history), stop on a refusal, echo the response blocks into the transcript, and execute any tool_use calls — each mutating one through the approval gate first. All tool_result blocks for one round-trip travel back in one user message.

The loop is non-streaming: each turn is one request, which keeps the assistant echo (thinking signatures, tool_use inputs) exact. Prose does not appear token by token; the viewer shows a live status row and a busy marker so a long turn does not read as frozen.

The tool catalog is a static list, byte-identical across turns, and the system prompt carries a cache breakpoint at its end — together they keep the stable prefix cacheable as the history grows.

The cmdlet constructs a zero-argument AnthropicClient: the SDK resolves ANTHROPIC_API_KEY, then ANTHROPIC_AUTH_TOKEN, then an ant auth login profile. An unset environment variable does not mean unauthenticated. A client that cannot be constructed at all fails with an AuthenticationError terminating error.

A verified end-to-end run, against anthropic/claude-sonnet-4.6 through OpenRouter, fixing a one-line bug in a Python file:

- auth: OPEN_ROUTER_KEY in .env.local (via openrouter.ai) -> https://openrouter.ai/api
> Read calc.py, fix the bug in add(), then run it to prove the fix. Keep it short.
· Let me read the file first.
⚙ read_file calc.py ✔ read calc.py (33 chars)
● Bug is clear: `-` should be `+`. Fixing it now.
⚙ edit_file calc.py ✔ edit calc.py
⚙ run_bash python -c "..." ✔ exit 0
● Fixed. add() was using `-` instead of `+`.
- done · 5802 in / 332 out tokens

That covers the tool loop, the assistant echo, the tool_result round-trip and four model round-trips in one turn.

Every path operand is confined to WorkspaceRoot by WorkspacePath.TryResolve; ../../.ssh/id_rsa is refused by the executor, not the prompt. edit_file and run_bash ask before they run — a modal chooser in the viewer with Allow once / Allow everything this session / Refuse. “Allow everything this session” currently behaves as a single allow; -AutoApprove is the durable form. Without a terminal and without -AutoApprove, mutating tools are refused outright: a prompt that cannot be shown cannot be answered. Details: Tools & Safety.