Harness Notes

Environment detection

Updated 2026-11-22,

On this page

How a script, hook or CLI can tell it was started by each harness, and which session started it. Children inherit these variables, so treat them as hints that an agent is involved, not as proof that no person is watching.

Detecting the harness

QuestionOpenCodechecked at 2.1.4
The variable to check

OPENCODE=1 and AGENT=1, plus AI_AGENT=opencode unless AI_AGENT is already set.

Because an existing AI_AGENT is kept, OpenCode running inside another agent passes the outer agent's name through.

Which processes get it

Partly

Commands the agent runs with its shell tool. Terminals OpenCode opens get only OPENCODE_TERMINAL=1, which agent commands also carry.

Which session started the command

OPENCODE_SESSION_ID in agent shell commands.

Relying on it

QuestionOpenCodechecked at 2.1.4
Telling the agent's commands from yours

Partly

Use OPENCODE or AGENT: OpenCode's terminals don't get them. Don't use OPENCODE_TERMINAL, which both get.

Signs that nobody will answer a prompt

Partly

Agent commands run with standard input closed, so a prompt reads end-of-file. TERM is xterm-256color, not a dumb terminal.

Removing or overriding it

Partly

A plugin's shell create.before hook can edit the environment, but the shell tool sets OPENCODE, AGENT and OPENCODE_SESSION_ID after the hooks run, so only AI_AGENT can be changed that way.

Notes and sources

OpenCode, 4 sources
The variable to check, 2
Which processes get it, 3
Which session started the command, 1
Telling the agent's commands from yours, 2
Signs that nobody will answer a prompt, 1
Removing or overriding it, 2