Skip to content

Commit 3ddec4d

Browse files
committed
Add --max-tokens param to cagent new to allow manual context length overriding
Signed-off-by: Christopher Petito <chrisjpetito@gmail.com>
1 parent 533ae73 commit 3ddec4d

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,22 +208,28 @@ To use the feature, you must have an Anthropic, OpenAI or Google API key availab
208208

209209
You can choose what provider and model gets used by passing the `--model provider/modelname` flag to `cagent new`
210210

211-
If `--model` is unspecified, `cagent new` will automatically choose between these 3 providers in order based on the first api key it finds in your environment
211+
If `--model` is unspecified, `cagent new` will automatically choose between these 3 providers in order based on the first api key it finds in your environment.
212212

213213
```sh
214214
export ANTHROPIC_API_KEY=your_api_key_here # first choice. default model claude-sonnet-4-0
215215
export OPENAI_API_KEY=your_api_key_here # if anthropic key not set. default model gpt-5-mini
216216
export GOOGLE_API_KEY=your_api_key_here # if anthropic and openai keys are not set. default model gemini-2.5-flash
217217
```
218218

219-
Example of provider and model overriding:
219+
`--max-tokens` can be specified to override the context limit used.
220+
When using DMR, the default is 16k to limit memory usage. With all other providers the default is 64k
221+
222+
Example of provider, model and context size overriding:
220223

221224
```sh
222225
# Use GPT-5 via OpenAI
223226
cagent new --model openai/gpt-5
224227
225228
# Use a local model (ai/gemma3-qat:12B) via DMR
226229
cagent new --model dmr/ai/gemma3-qat:12B
230+
231+
# Override the max_tokens used during generation, default is 64k, 16k when using the dmr provider
232+
cagent new --model openai/gpt-5-mini --max-tokens 32000
227233
```
228234

229235
---

cmd/root/new.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import (
1313
"github.com/docker/cagent/pkg/runtime"
1414
)
1515

16-
var modelParam string
16+
var (
17+
modelParam string
18+
maxTokensParam int
19+
)
1720

1821
// Cmd creates a new command to create a new agent configuration
1922
func NewNewCmd() *cobra.Command {
@@ -90,7 +93,7 @@ func NewNewCmd() *cobra.Command {
9093
fmt.Println()
9194
}
9295

93-
out, err := creator.StreamCreateAgent(ctx, ".", prompt, runConfig, modelProvider, model)
96+
out, err := creator.StreamCreateAgent(ctx, ".", prompt, runConfig, modelProvider, model, maxTokensParam)
9497
if err != nil {
9598
return err
9699
}
@@ -131,6 +134,7 @@ func NewNewCmd() *cobra.Command {
131134
}
132135
addGatewayFlags(cmd)
133136
cmd.PersistentFlags().StringVar(&modelParam, "model", "", "Model to use, optionally as provider/model where provider is one of: anthropic, openai, google, dmr. If omitted, provider is auto-selected based on available credentials or gateway")
137+
cmd.PersistentFlags().IntVar(&maxTokensParam, "max-tokens", 0, "Override max_tokens for the selected model (0 = default)")
134138

135139
return cmd
136140
}

docs/USAGE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ $ cagent api config.yaml --port 8080
5050

5151
# Project Management
5252
$ cagent new # Initialize new project
53+
$ cagent new --model openai/gpt-5-mini --max-tokens 32000 # Override max tokens during generation
5354
$ cagent eval config.yaml # Run evaluations
5455
$ cagent pull docker.io/user/agent # Pull agent from registry
5556
$ cagent push docker.io/user/agent # Push agent to registry

internal/creator/agent.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func CreateAgent(ctx context.Context, baseDir, prompt string, runConfig latest.R
120120
return messages[len(messages)-1].Message.Content, fsToolset.path, nil
121121
}
122122

123-
func StreamCreateAgent(ctx context.Context, baseDir, prompt string, runConfig latest.RuntimeConfig, providerName, modelNameOverride string) (<-chan runtime.Event, error) {
123+
func StreamCreateAgent(ctx context.Context, baseDir, prompt string, runConfig latest.RuntimeConfig, providerName, modelNameOverride string, maxTokensOverride int) (<-chan runtime.Event, error) {
124124
defaultModels := map[string]string{
125125
"openai": "gpt-5-mini",
126126
"anthropic": "claude-sonnet-4-0",
@@ -168,6 +168,9 @@ func StreamCreateAgent(ctx context.Context, baseDir, prompt string, runConfig la
168168
if providerName == "dmr" {
169169
maxTokens = 16000
170170
}
171+
if maxTokensOverride > 0 {
172+
maxTokens = maxTokensOverride
173+
}
171174

172175
llm, err := provider.New(
173176
ctx,

0 commit comments

Comments
 (0)