Dev.to AI 🤖 Ai 👁 0 📖 9 min read

Choose Free, Paid, or Self-Hosted by the Output Contract You Can Observe

Choose Free, Paid, or Self-Hosted by the Output Contract You Can Observe Choosing between a free hosted tier, a paid plan, and a self-hosted deployment is often framed as a model-quality or price question. A more usefu

Choose Free, Paid, or Self-Hosted by the Output Contract You Can Observe

Choosing between a free hosted tier, a paid plan, and a self-hosted deployment is often framed as a model-quality or price question. A more useful first question is narrower: what can your automation observe at the process boundary, and is that enough to meter, retry, and audit the work without a human watching? If the honest answer is 'an exit code and one stream of text', then you are comparing vendors against a contract you have not verified.

This guide is for engineers who wrap a non-interactive AI CLI inside CI jobs, cron entries, or queue workers. It assumes you can run the CLI from a shell, redirect its streams, and ship logs somewhere. It does not assume any particular provider's pricing, quota, or model quality. The checks below are proposed observations to make against your own provider and version; no code or command in this article was executed.

The observable surfaces at the process boundary

According to the Codex CLI documentation, in its default output mode, codex exec writes progress to stderr and sends the final agent message to stdout. Running the same command with --json changes stdout into a JSON Lines event stream, where each line is a JSON object (non-interactive mode docs). Those two statements define the decision problem, because they define what your wrapper can and cannot see without extra instrumentation.

At the process boundary, a wrapper can directly observe three things: the exit status, stdout, and stderr. Exit status tells you whether the process considered itself successful. Stdout carries the deliverable in default mode, or the event stream in JSON mode. Stderr carries narration about how the deliverable was produced. Everything you later want to meter, alert on, or bill against has to be derived from those three things plus whatever you add around them.

Why JSON mode changes the shape of your artifact

The moment you add --json, stdout stops being 'the answer' and becomes a JSON Lines event stream. A wrapper that redirects stdout to an answer file therefore produces two different artifacts depending on a single flag. Downstream code that assumed one shape may fail quietly on the other. This mismatch can produce confusing 'empty result but exit code zero' reports in agent automation.

An observation-based test for the artifact shape is enough to expose the problem before you build on it. Use a short prompt whose final message you can recognize, such as a request to summarize a small changelog into a fixed number of bullets. Capture stdout and stderr separately. In default mode, observe whether stdout contains only the final message and whether stderr contains progress narration. Then invoke the CLI once with the documented JSON mode and observe whether stdout is a sequence of JSON objects, one per line. A mismatch is any of the following: default stdout is JSON Lines; JSON-mode stdout is plain final message; progress narration appears mixed into the artifact you intended to parse; or the final message cannot be located from the documented event schema. If any of those happens, your parser assumptions are wrong, and you should not proceed to metering or retry logic until the artifact shape is pinned to a documented version.

Do not infer the event field names from this article. The documentation says each line is a JSON object; it does not make this article's illustrative field names contractual. Extract the final message once, store it explicitly, and keep the raw stream for inspection.

A five-step wrapper design you can check

  1. Separate the streams before you parse anything. Redirect stdout and stderr to different files or different log sinks, and do not merge them in a job whose output you intend to machine-read. Progress narration is not a deliverable, and mixing it into the deliverable makes every downstream parser fragile. Verification: inspect both sinks after a default-mode run and confirm that the deliverable and the narration are not interleaved.

  2. Decide which artifact is contractual. Write down whether your system consumes the final message only, or the full event stream. If it consumes the stream, treat the final message as a derived value that you extract once and store explicitly. Verification: a reviewer should be able to point to the stored artifact and say whether it is the final message or the raw event stream, without reading wrapper code.

  3. Parse defensively, including the last line. A truncated final line is a real failure mode when a process is killed mid-write, so a parser that silently skips malformed lines can hide the exact incident you care about. Fail loudly on unparseable input and keep the raw file for inspection. Verification: feed the parser a deliberately truncated JSON Lines file and confirm it reports an error rather than returning an empty success.

  4. Classify failures into at least three buckets. Distinguish 'process exited non-zero', 'process exited zero but produced no final message', and 'process produced a final message that failed your own validation'. A naive wrapper that checks only exit status sees only the first bucket. The other two are where agent automation often loses trust. Verification: for each bucket, record the observed exit status, the presence or absence of a final message, and the validation result; a bucket that cannot be distinguished in your logs is a gap.

  5. Attach metering at the boundary you control. Count invocations, wall-clock time, and artifact size in your wrapper rather than relying on a dashboard you do not own. This is what makes a later free-versus-paid comparison honest, because you are comparing your own measured demand against a vendor's published terms. Verification: after a fixed observation window, reconcile your invocation count with the provider's usage report if one exists, and record any unexplained difference as an open question rather than an assumption.

The decision matrix

Criterion Free hosted tier Paid hosted plan Self-hosted deployment
What you must be able to observe Exit status and final message, at minimum Same, plus whatever usage signal the plan exposes Same, plus your own service metrics
What you own The wrapper and the logs The wrapper, the logs, and the billing relationship The wrapper, the logs, the runtime, and the upgrade path
Typical failure mode Limits you cannot see from inside the process Cost that grows without a per-task ceiling Operational burden that outlives the original problem
Good fit when Volume is low and a human reviews output Volume is predictable and the wrapper is already metered You need data-flow control or offline execution
Poor fit when Output feeds an unattended pipeline You cannot attribute cost per task Nobody owns patching and capacity

Read the table as fit criteria, not as a ranking. A free tier is a reasonable choice when a human reads every result anyway, because the observable contract only needs to be as strong as the review step behind it. Self-hosting becomes the right answer when the constraint is where data flows, not what the model can do.

The edge case: credentials that travel through stderr

Progress logs are tempting to ship to a third-party aggregator, and that is where a subtle problem appears. RFC 6750 describes bearer tokens as credentials that any party possessing them can present without proving possession of a separate cryptographic key, and it notes that token disclosure can permit unintended use (RFC 6750). If your wrapper exports an API token into the environment and the CLI narrates request context to stderr, your progress log may contain a credential or request context that should not be shipped unredacted.

Self-hosting does not automatically fix this, because the boundary you moved is the model endpoint, not your logging pipeline. Treat the log sink as part of the trust boundary: redact known token patterns before shipping, keep stderr retention short, and confirm that token transmission uses TLS as RFC 6750 requires. A decision guide that ignores log egress can pick the wrong deployment for the wrong reason.

Retries need a side-effect check, not just a backoff

A wrapper that retries on any non-zero exit may repeat a write it already performed. HTTP idempotency concerns the intended effect on the server, and responses to repetitions can differ; RFC 9110 advises retrying a non-idempotent request automatically only with knowledge that its semantics permit replay or that the original was never applied (RFC 9110). Retries can also add load to an overloaded dependency; backoff spaces attempts and jitter reduces clients retrying in lockstep, but neither mechanism proves that the preceding call had no side effects (AWS builders' library).

Verification for this edge case: use a task that creates or appends to a record in a test system, induce a failure after the side effect would have occurred, and observe whether the wrapper retries. If it retries, check whether the record was duplicated or the operation was replayed. A mismatch is any retry of a non-idempotent operation without an explicit replay contract. The correct outcome is not 'retry harder'; it is 'classify the operation and only retry when the semantics allow it'.

How to verify the contract before you commit

These are proposed checks, not results from a completed test run, and you should execute them against your own provider and version.

  1. Run a deliberately failing prompt and confirm the exit status is non-zero and distinguishable from a validation failure. Mismatch: exit status is zero while no valid deliverable exists, or the failure is indistinguishable from a validation failure in your logs.

  2. Invoke the CLI once in default mode and once with the documented JSON mode, then compare the artifacts. Observe whether default stdout is plain final message and JSON-mode stdout is JSON Lines. Mismatch: the parser accepts the wrong shape or silently extracts nothing.

  3. Kill the process mid-run and confirm your parser reports truncation instead of returning an empty success. Mismatch: a partial JSON line is skipped and the job is marked successful.

  4. Grep both log files for token prefixes and confirm nothing sensitive reaches the sink you ship to. Mismatch: a token-shaped string appears in a log that leaves your trust boundary.

  5. Record invocation counts for a week and compare that measured demand against the published terms of each option. Mismatch: your measured demand and the provider's usage report cannot be reconciled, or the comparison relies on a quota you have not verified.

Failure modes worth naming

Silent success with an empty deliverable is expensive, because it looks like a completed job in every dashboard. Retry storms on non-idempotent side effects are another, since a wrapper that retries on any non-zero exit may repeat a write it already performed. Assuming self-hosted means private is a third, because external model or tool providers may still receive data depending on how the deployment is wired.

When this framework does not apply

If you run the CLI interactively, the stream contract is largely irrelevant and the human is the parser. If the provider does not document non-interactive output behavior for your version, you cannot build a decision on it and should either pin a version whose behavior is documented or keep a human in the loop. And if your task volume is a handful per day, the wrapper design may cost more than the decision it informs.

AI assistance was used to draft this article. The checks above are proposed and were not executed, and no provider behavior, quota, or pricing is asserted beyond the linked documentation. A human should verify every technical claim, the current provider documentation, and DEV's current policy before publishing.

This article was created with AI assistance.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.