Clatter LabsHelp Center

How do I read Line CLI's JSON output from a script?

How-toUpdated

Example in Python:

import json, subprocess

p = subprocess.run(
    ["line", "-p", "Summarize the open TODOs", "--output-format", "json", "--permission-mode", "dont-ask"],
    capture_output=True, text=True,
)
result = json.loads(p.stdout.strip().splitlines()[-1])
if result["is_error"]:
    print("failed:", result["subtype"], result["error"])
else:
    print(result["result"])
    print("tokens in/out:", result["usage"]["input_tokens"], result["usage"]["output_tokens"])

Also check p.returncode: 0 success, 1 error, 2 max turns, 130 interrupted. With stream-json, parse each line as its own event.

Related articles

Still need help?

Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyz
For deleting your whole account or the data of one service.