claudecode

package
v1.0.0-alpha.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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"
)
View Source
const SDKName = "claude-code-sdk"

SDKName is the costroid/agent tag of a program that runs Claude Code through the Claude Agent SDK.

View Source
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

func ReadExecutionAnswer(file io.Reader) (string, error)

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

func ReadStreamAnswer(stream io.Reader) (string, error)

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

func (a Agent) Claim() (func(), error)

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

func (Agent) GatewayArgs(string) []string

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

func (Agent) GatewayBypass(argv []string) (string, error)

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

func (Agent) GatewayEnv(base, key string) []string

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) Name

func (Agent) Name() string

Name returns the costroid/agent tag.

func (Agent) NewSession

func (a Agent) NewSession(adapter.Plan) adapter.Session

NewSession starts reading a run.

func (Agent) Plan

func (Agent) Plan(argv []string, budget decimal.Decimal) (adapter.Plan, error)

Plan adds the stream flags and the budget flag.

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

func (a Agent) ReadExecutionFile(file io.Reader, saved time.Time) (adapter.Report, error)

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

func (a SDKAgent) Claim() (func(), error)

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

func (SDKAgent) GatewayArgs(string) []string

GatewayArgs is empty: the program's command line is not Claude Code's.

func (SDKAgent) GatewayBypass

func (a SDKAgent) GatewayBypass([]string) (string, error)

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

func (a SDKAgent) GatewayEnv(base, key string) []string

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) Name

func (SDKAgent) Name() string

Name returns the costroid/agent tag.

func (SDKAgent) NewSession

func (a SDKAgent) NewSession(adapter.Plan) adapter.Session

NewSession starts reading a run. It reads the environment the program inherits once: the CI system, ENABLE_PROMPT_CACHING_1H and the API key.

func (SDKAgent) Plan

func (SDKAgent) Plan(argv []string, _ decimal.Decimal) (adapter.Plan, error)

Plan runs the program as it is. Its budget is the program's own maxBudgetUsd, which no command line states.

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

func (s *Session) Feed(line []byte, at time.Time) error

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

func (s *Session) Finish() (adapter.Report, error)

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

func (s *Session) RunWindow(start, end time.Time)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL