Skip to content

Output contract for agents: next commands, error hints, one list shape, commands --json #160

Open

Description

@ertembiyik

tg's main users are agents, and the envelope (success, data or error, stderr for errors, documented exit codes) already suits them. This issue proposes six additions to that contract. Each one comes from something agents got wrong while using tg, not from taste, and each would be its own small PR. Tell me which ones you want.

Where the evidence comes from

  • Usage: transcripts of 27 agent sessions on my machine over three weeks (Claude Code and Codex, tg 0.5 to 0.10), read for every tg call, its result and what the agent did next. Counts below are from those. Anything marked "0.12.1" I rechecked on the current release.
  • Code: counts on main at faf505d.
  • Precedent: other CLIs built for agents, checked in their current source, plus two guides: clig.dev and Anthropic's Writing tools for agents.

1. next: runnable commands in the answer

Evidence

  • A search on an empty cache answers with success and the fix inside a free-text field: {"results":[],"total":0,"source":"cache","message":"No results in cache. Run \"tg chats list --fresh\" to populate cache first."}. Agents read this as "nothing found" 13 times in 5 sessions before one of them noticed the message.
  • Every deep read of a chat was a hand-written shell loop that rebuilt the command from nextBefore. One loop passed the flag wrongly and fetched the same 100 messages 80 times (a quoting mistake on the agent's side, but there was no command to copy).

Precedent: Vercel's agent envelope has next?: Array<{ command, when? }> (agent-output.ts); Basecamp's has breadcrumbs: [{ action, cmd }] (envelope.go); clig.dev: "suggesting to the user commands they can run next helps them learn how to use your program."

Proposal: an optional next: [{ command, why }] beside data. First uses: the next page of a list, and the --fresh command when the cache has nothing. Additive.

2. Errors that say what to do, and whether to retry

Evidence

  • error() takes a suggestion, and AGENTS.md lists it in the stable error schema, but 20 of 134 calls pass one. By code: TELEGRAM_ERROR 0 of 24, RATE_LIMITED 0 of 9, INVALID_ARGS 7 of 60. (Finish the AI integration (skills) #3 notes the same gap.)
  • tg api with {"peer":"@name"} fails with Unknown object undefined. That happened 7 times in 4 sessions, and each agent had to work out contacts.resolveUsername by itself.
  • 63 downloads run four at a time: 7 failed during a flood-wait and all 7 passed when rerun one by one. Nothing in the error said a retry was safe or when.

Precedent: Basecamp errors are { error, code, retryable, hint } (same file); Vercel has hint; Anthropic's guide: "prompt-engineer your error responses to clearly communicate specific and actionable improvements."

Proposal: set suggestion on the errors agents hit most (unresolved peers in tg api, missing arguments, empty cache, daemon not running), and add retryable plus the wait in seconds where Telegram gives one (details.wait_seconds already has it). Additive.

3. One shape for every list

Evidence (0.12.1, real output):

Command List key Paging Date field
chats list items offset, limit, hasMore lastMessageAt, milliseconds
chats search results none lastMessageAt, milliseconds
messages list items before, after, nextBefore, nextAfter date, seconds
messages search results limit, offset, total date, seconds
contacts list items offset, limit, hasMore —

tg status and tg sql rows are snake_case; message and chat items are camelCase.

Agents in 6 sessions wrote fallbacks such as .items // .chats // .results, one script crashed reading items from the wrong level, and one query guarded against both date units in a single expression.

Precedent: clig.dev: "Use the same flag names for the same things, have similar output formatting"; the Vercel and Basecamp envelopes above are one shape for every command.

Proposal: every list answers with items and one paging block, and every timestamp uses one unit. This one breaks existing scripts, so it needs your call on how: a major version, or the schema name in point 6, which lets a new shape ship beside the old one.

4. tg commands --json

Evidence: #3 already says tg skill install writes a skill.json that no agent reads. In the sessions, agents learned tg by calling --help on one subcommand after another (dozens of calls), and still guessed flag names: --chat on chats get, which takes --id, and a positional query before tg sql accepted one.

Precedent: Resend ships a commands and a doctor command (src/commands); gh pr list --json with no fields prints the fields it accepts, so the flag documents itself.

Proposal: one command that prints every command, its flags, the error codes it can return and the name of its output shape, from the installed version. The skill in #3 can then be checked against it in CI.

5. Declare each command's output once

Evidence: drift between what is documented and what the code does is already visible: the suggestion field in point 2, and in #159 one new field had to be described by hand in four places (docs/cli-commands.md, docs/database-schema.md, the schema annotation and the TypeScript type).

Proposal: one declaration per command's data that produces the type, the docs table and the entry in tg commands --json, with an e2e check that real output matches it. Which library, if any, is your choice; the point is one source. I would not validate tg's own output at runtime or run a schema over every row of a large read.

6. A schema name in each answer

Why it is needed

  • What an agent knows about tg is older than the binary it runs: it comes from a skill, the docs or an earlier session. Today nothing in an answer says which shape it is, and agents rarely call tg --version first.
  • The shapes do change. tg shipped nine releases between 28 October and 13 November, and the sessions above ran three of them (0.5, 0.8, 0.10). Between those, tg media appeared, chats list gained syncEnabled and syncReason, and tg sql began to accept a positional query. An agent that learned one version guessed wrong on another.
  • Two installs can differ at the same moment: 0.11.0 reached npm before the Homebrew tap.

Proposal: a schema field beside success that names the shape and its version, for example tg.messages.list/1. A shape changes only with a new number. That gives point 3 a path that breaks nobody (ship /2, keep /1 for a release), lets point 4 list the shapes, and lets the skill in #3 say which versions it was written for. Additive.

Not proposed

Suggested order

2, then 1, then 6, then 4 with 5, and 3 last, since it is the only breaking one. I'm happy to send each as its own PR once you say which you want and how you'd like 3 handled.

Activity

  1. neoromantic commented on Nov 17, 2026

    @neoromantic
    Owner

    Thanks for going through those sessions. The repeated page fetch and the empty-cache searches make a good case for these changes.

    1. Yes to next for pagination and empty caches. Please carry the account and filters into the suggested command.
    2. Yes to actionable errors. I'd start here, especially with flood waits where we know when a download can be retried. Separate PRs for 1 and 2 would be great.
    3. Go ahead with consistent lists and timestamps. Please include a before/after example and show how an existing script upgrades. A change from seconds to milliseconds can silently give it the wrong date.
    4. Would commands, arguments and flags from the installed CLI cover the discovery problems you saw? What would an agent do differently if it also knew every possible error and schema name? Those lists could drift unless we can derive them.
    5. A single output declaration makes sense to me. Please try it on one command so we can see which types and docs it replaces and how the e2e check catches drift.
    6. I'm undecided. A schema name tells a script which shape it received. If both versions ship, how does an existing script keep getting v1, and how does a new one request v2? Let's work that out with the example in point 3.
  2. ertembiyik commented on Nov 18, 2026

    @ertembiyik
    ContributorAuthor

    Thanks. 1 and 2 are up as separate PRs: #176 (next, carrying the account and every filter) and #177 (errors, starting with flood waits).

    On 3, the simple version.

    • Lists: the two search commands return items instead of results, like every other list. One PR, marked breaking, with the before/after and the one-line script change (.results becomes .items).
    • Timestamps: I'd change nothing. date and editDate are Telegram's own field names and carry Telegram's seconds; the fields tg adds itself (lastMessageAt, the send result's timestamp) are milliseconds, as the schema docs already say for the database. I'd only state that rule in the output docs, so an agent doesn't have to guess.

    On 4: agreed, commands, arguments and flags are enough. What I saw was agents guessing flag names (--chat vs --id, a positional query vs --query) and calling --help on one subcommand after another. I have no evidence that listing errors or schema names changes what an agent does, so let's drop both. It derives from the citty command tree, plus read or write from COMMAND_ACCESS, so nothing is kept by hand.

    On 5: I tried it on tg chats list: #179. One 82-line declaration replaces three hand-written types and the field prose in the docs (which covered 3 of 16 fields) with a generated table, and an e2e test parses the real output strictly against it; the PR shows the failing output when a field is renamed without the declaration. It uses zod as a devDependency only, so nothing new ships in the binary. The output is byte-identical.

    On 6: let's drop it. If 3 ships as one documented breaking change, we don't need schema names or two shapes side by side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions