Documentation
¶
Overview ¶
Package claudecode reads Claude Code runs.
The stream supplies the session id, the billing source and the result's per-model totals. Its assistant messages carry a placeholder output count, so per-request rows come from the session files Claude Code writes, checked against those totals. Without the files a run falls to one set of rows per model.
Index ¶
- Constants
- func ReadExecutionAnswer(file io.Reader) (string, error)
- func ReadStreamAnswer(stream io.Reader) (string, error)
- type Agent
- func (a Agent) Claim() (func(), error)
- func (Agent) GatewayArgs(string) []string
- func (Agent) GatewayBypass(argv []string) (string, error)
- func (Agent) GatewayEnv(base, key string) []string
- func (Agent) GatewaySettings(argv []string, base, key string) (args []string, remove func() error, err error)
- func (Agent) Name() string
- func (a Agent) NewSession(adapter.Plan) adapter.Session
- func (Agent) Plan(argv []string, budget decimal.Decimal) (adapter.Plan, error)
- func (a Agent) ReadCappedExecutionFile(file io.Reader, saved time.Time, stated Cap) (adapter.Report, error)
- func (a Agent) ReadExecutionFile(file io.Reader, saved time.Time) (adapter.Report, error)
- type Cap
- type SDKAgent
- func (a SDKAgent) Claim() (func(), error)
- func (SDKAgent) GatewayArgs(string) []string
- func (a SDKAgent) GatewayBypass([]string) (string, error)
- func (a SDKAgent) GatewayEnv(base, key string) []string
- func (SDKAgent) Name() string
- func (a SDKAgent) NewSession(adapter.Plan) adapter.Session
- func (SDKAgent) Plan(argv []string, _ decimal.Decimal) (adapter.Plan, error)
- type SDKSession
- type Session
Constants ¶
const ( // Name is the costroid/agent tag. Name = "claude-code" // SourceStream marks usage read from the result object alone. SourceStream = "agent_stream" // SourceSessionFile marks usage read from Claude Code's session files. SourceSessionFile = "agent_session_file" )
const SDKName = "claude-code-sdk"
SDKName is the costroid/agent tag of a program that runs Claude Code through the Claude Agent SDK.
const SourceExecutionFile = "execution_file"
SourceExecutionFile marks usage read from the execution_file output of claude-code-action.
Variables ¶
This section is empty.
Functions ¶
func ReadExecutionAnswer ¶
ReadExecutionAnswer returns the answer a claude-code-action run ended with: the result field of the last result message in its execution file, the array of SDK messages the action wrote.
func ReadStreamAnswer ¶
ReadStreamAnswer returns the answer a claude -p run ended with: the result field of the last result message on its stream-json output. Lines that are not JSON objects are passed over, and so is every message but the result.
Types ¶
type Agent ¶
type Agent struct {
// ConfigDir holds Claude Code's sessions. Empty means CLAUDE_CONFIG_DIR,
// then ~/.claude.
ConfigDir string
}
Agent is Claude Code.
func (Agent) Claim ¶
Claim holds the config directory for one run. A run reads every session in the directory that starts and ends inside it as nested, so a second run in the same directory would be counted twice: once by itself, and once by the run around it. The lock is the kernel's, so it ends with the process that held it, however that process ends.
func (Agent) GatewayArgs ¶
GatewayArgs is empty. Claude Code's command line names the gateway through the settings file GatewaySettings writes, because that file holds the key.
func (Agent) GatewayBypass ¶
GatewayBypass names the first flag of the command that would send Claude Code's requests somewhere other than the gateway on the run's key: a flag that starts the session in the cloud, or a --settings value whose env sets a routing variable. It reads the command up to a "--" that ends the options.
func (Agent) GatewayEnv ¶
GatewayEnv points Claude Code at the gateway, with the run's key as its bearer token. An API key beside it would let a request skip the gateway, so it is removed.
func (Agent) GatewaySettings ¶
func (Agent) GatewaySettings(argv []string, base, key string) (args []string, remove func() error, err error)
GatewaySettings writes the settings file a gateway run gives Claude Code, and returns the arguments that name it and the function that removes it.
Claude Code applies a project's .claude/settings.json env over the environment it starts with, and a --settings file's env over both, one variable at a time. Of several --settings it reads the last alone. So the file holds the command's own last --settings, every key as written, with the gateway's base URL, the run's key, no API key and no other provider set in its env, and an empty apiKeyHelper. A repository's .claude/settings.json can name a helper, which Claude Code runs and whose output it sends as x-api-key beside the run's bearer token, so the run's file names none. The run places its arguments after every option the command holds. The file is new, and only the run's user can read it.
func (Agent) NewSession ¶
NewSession starts reading a run.
func (Agent) ReadCappedExecutionFile ¶
func (a Agent) ReadCappedExecutionFile(file io.Reader, saved time.Time, stated Cap) (adapter.Report, error)
ReadCappedExecutionFile reads the file as ReadExecutionFile does, and grades the run by the cap the workflow states and what the file shows.
func (Agent) ReadExecutionFile ¶
ReadExecutionFile reads the array of SDK messages claude-code-action wrote. The file holds prompts and tool output, and only usage fields leave it.
The messages are the ones Claude Code streams, so the same session reads them, and the last result's per-model totals are the run. Costroid did not start this run and is told no cap, so the grade is none.
type Cap ¶
type Cap struct {
// USD is the cap the read stage was given. Nil when it was given none.
USD *decimal.Decimal
// Args is the claude_args the step was given, as the read stage was told
// them. Empty when it was told none.
Args string
}
Cap is what a workflow states about the cap a claude-code-action run was held to. The read stage never sees the step's inputs, so both are the workflow's own statements.
type SDKAgent ¶
type SDKAgent struct {
// ConfigDir holds Claude Code's sessions. Empty means CLAUDE_CONFIG_DIR,
// then ~/.claude.
ConfigDir string
// Dir is the run's working directory. Empty means the process's own.
Dir string
}
SDKAgent is a program that runs Claude Code through the Claude Agent SDK. The program's stdout is its own, so the run reads none of it. Every Claude Code session that started and ended while the program ran is the run's: in CI a session in any project directory, outside CI one whose working directory is the run's.
func (SDKAgent) Claim ¶
Claim holds the config directory for one run, as a run of Claude Code does, so a second run there cannot count this run's sessions as its own.
func (SDKAgent) GatewayArgs ¶
GatewayArgs is empty: the program's command line is not Claude Code's.
func (SDKAgent) GatewayBypass ¶
GatewayBypass names the first settings file the Claude Code the program starts may read that sends its requests around the gateway: one whose env sets a variable that picks where requests go, or that names a key helper. The run cannot give that Claude Code a --settings of its own to override them, so it refuses the run instead. The command line itself is the program's and is not read.
func (SDKAgent) GatewayEnv ¶
GatewayEnv points Claude Code at the gateway, with the run's key as its bearer token. No command line reaches the Claude Code the program starts, so the environment also empties every other variable that picks where its requests go.
func (SDKAgent) NewSession ¶
NewSession starts reading a run. It reads the environment the program inherits once: the CI system, ENABLE_PROMPT_CACHING_1H and the API key.
type SDKSession ¶
type SDKSession struct {
// contains filtered or unexported fields
}
SDKSession reads the Claude Code sessions of one program's run.
func (*SDKSession) BilledThroughGateway ¶
func (s *SDKSession) BilledThroughGateway()
BilledThroughGateway reads the run as key-billed: the gateway bills every request per token on its own provider key.
func (*SDKSession) Feed ¶
func (*SDKSession) Feed([]byte, time.Time) error
Feed passes over a line of the program's stdout. The program prints what it chooses, so nothing in it is read as usage.
func (*SDKSession) Finish ¶
func (s *SDKSession) Finish() (adapter.Report, error)
Finish reads every session under the config directory's projects that started and ended inside the run. The earliest names the run, and each other one is reported under its own session id. A session that wrote inside the run and fails a rule is named in a warning, with the reason.
func (*SDKSession) RunWindow ¶
func (s *SDKSession) RunWindow(start, end time.Time)
RunWindow records when the program ran. Only a session inside it counts.
The run takes both ends from the system's wall clock, the clock Claude Code dates each session line by. Claude Code states a line's time in whole milliseconds, cut down, so a line written in the run's first millisecond reads up to 1 ms before the run's start. The start is therefore taken down to its whole millisecond: that is the window's only slack, under 1 ms. The end needs none: a line's time never reads later than when it was written, and every line is written before the program ends.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session reads one Claude Code run.
func (*Session) BilledThroughGateway ¶
func (s *Session) BilledThroughGateway()
BilledThroughGateway reads the run as key-billed: the gateway bills every request per token on its own provider key, whatever apiKeySource Claude Code states for the run's key.
func (*Session) Feed ¶
Feed reads one stdout line. A line that is not JSON is not the agent's stream and carries no usage. The line's type is read first, then the fields that type carries. A line whose shape this reader does not know is skipped and counted, unless it is a result: the result holds the run's figures, so a result that cannot be read stops the run's reading at Finish.
func (*Session) Finish ¶
Finish returns per-request usage when the session files agree with the result, and per-model usage otherwise, then the nested sessions the run's project directory holds.
func (*Session) RunWindow ¶
RunWindow records when the agent's process ran. Without it, the window is the span of the stream lines the session was fed.
Claude Code states a line's time in whole milliseconds, cut down, so a line written in the run's first millisecond reads up to 1 ms before the run's start. The start is therefore taken down to its whole millisecond, as the SDK session's is.