Example output:
Error: Output for session 'shell' contained a line exceeding the max of 1600 bytes (observed at least 2599 bytes).
The byte sequence which exceeded the limit started with: b'2732:<div class="pro'
The exec session has been deleted. Please start a new session.
this causes the agent to start the session from scratch. Such long lines can appear when it tries to run grep
over HTML files which can often have long lines without newlines, e.g. when inline JS is present.
I think ideally it should not terminate the session at the very least, and instead try to modify the command to make output smaller.
I’m having the same issue. In the end, I found out to use the python request for web search.
You can add the below md to your Agents.md and check the log to see wether it’s working as your expected. It will auto install dependancies itself if needed. You can also add those library into your environment to speed up this.
Large Output & Context‑Handling Guidelines
Codex sessions terminate if any single line in stdout exceeds 1 600 bytes. Follow the rules below to gather complete context safely.
- Fetch content programmatically first – Default to the Python pattern (
requests + BeautifulSoup + textwrap
) to download pages and wrap them before printing. This guarantees every line stays within the limit.
- Avoid Giant Single‑Line Prints – After parsing, wrap or slice the text (e.g.
textwrap.wrap(text, 1600)
or text[:4000]
) so no single line exceeds 1 600 bytes.
- Targeted Search First – Apply keyword filters (
if "keyword" in line
) or regex (re.findall
) on the parsed text to emit only the relevant snippets.
- Chunk / Page Large Outputs – Slice different segments (
text[start:end]
) instead of dumping the entire document in one go; this mimics scrolling without risk.
- Leverage Cached Knowledge – Prefer local docs under
knowledge/
to external sites. Add frequently‑used references there so future agents can read a safe, pre‑wrapped version.
- Complete Context over Speed – Take extra steps to gather the full context (multiple slices or filtered fetches) rather than answering prematurely with missing information.
- Stay within
.venv
- Install any extra parsing libs with uv pip install <pkg>
to keep the environment reproducible.
Python‑based Web Retrieval (Requests + BeautifulSoup + textwrap)
Use this pattern whenever a remote page is large, minified, or otherwise risky for direct shell output.
import requests, textwrap
from bs4 import BeautifulSoup
URL = "https://example.com/big-doc"
html = requests.get(URL, timeout=15).text # ① pull page
text = BeautifulSoup(html, "html.parser").get_text() # ② strip tags
safe = "\n".join(textwrap.wrap(text, width=1600)) # ③ hard‑wrap
print(safe[:4000]) # ④ print only what you need
Why it works
requests
downloads HTML with timeout control.
BeautifulSoup.get_text()
removes tags/scripts, shrinking long lines.
textwrap.wrap()
enforces the 1 600‑byte ceiling.
- Together, this prevents the “line too long” error while allowing keyword filtering and pagination.