Repository navigation
feat(api): pre-flight prompt-size cap with --max-input-tokens (#32) - #86
Merged
Merged
Conversation
The api agent pastes the brief plus target source into one prompt, so a large target produced a very large prompt whose cost was only known after the call. --max-cost is checked after the runs finish, so it can't stop that. Add --max-input-tokens on `secfoo run` (falls back to [agents.api].max_input_tokens). After the api agent assembles the prompt, it estimates its size with LiteLLM's tokenizer for the model (or ~4 chars per token when there is none). If the prompt is over the limit, the run fails before litellm.completion() is called, and the error says how large the prompt was and which limit applied. CLI agents read the target themselves, so they warn and ignore the setting instead of pretending to cap it. The other half of #32, a distinct exit code when --max-cost is exceeded, already shipped in #64 (exit 3). This also fixes `secfoo run --help`, which still said --max-cost exits 2. Closes #32 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Leaderboard update
karthickmk2001 now has 3 points (rank #3). 22 more to reach Bronze. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #32
What does this change, and why?
#32 asked for two things to make cost ceilings usable as a CI guard:
apiagent (this PR). Theapiagent pastes the brief plus the target's source into one prompt, so a large target produces a large prompt whose cost is only known after the call.--max-costis checked after the runs finish, so it can't prevent that.--max-costis exceeded. This already shipped in Use a distinct exit code (3) when a run stops at --max-cost #64 (exit3). This PR also fixessecfoo run --help, which still said--max-costexits2.How the cap works:
secfoo run --max-input-tokens <n>, falling back to[agents.api].max_input_tokensin~/.secfoo/config.toml.apiagent assembles the prompt (including incremental Memory Bank scans), it estimates the size with LiteLLM'stoken_counterfor the selected model. When LiteLLM can't count for that model, it uses ~4 characters per token, so the cap doesn't silently switch off.litellm.completion()is called, so nothing is sent or billed. The error names the size and the limit, for example: "Prompt is about 10,012 input tokens, over the max_input_tokens limit of 1,000 (--max-input-tokens or [agents.api].max_input_tokens). Nothing was sent to the model. Narrow the target with --exclude, or raise the limit."claude,codex, …) read the target's files themselves, so secfoo can't measure their prompt up front. They warn and ignore the setting rather than pretend to cap it.--modelfrom Support per-agent model selection and run overrides #60: CLI →execute_runs(max_input_tokens=…)→adapter.configure_max_input_tokens().Docs:
docs/REFERENCE.md(CI gates table and config example) andconfig.example.toml.Testing
pytest testspasses. 1027 passed locally. The only 4 failures are existing Windows-only POSIX permission and symlink tests that fail onmaintoo; Linux CI is unaffected.ruff check .adds no new findings in the changed files (same count asmain).tests/test_agents/test_api.py: over the limit fails with no model call; under the limit runs; no limit means no counting; LiteLLM's tokenizer is used for the selected model; the 4-chars-per-token fallback; invalid limits are rejected.tests/test_agents/test_base.py: CLI agents warn and ignore the setting.tests/test_settings.py: config parsing and validation.tests/test_cli.py: the flag overrides config, rejects 0, and--helpsays exit 3.🤖 Generated with Claude Code