Agent prepare call#229
Open
fwang2002 wants to merge 3 commits into
Open
Conversation
Lets applications attach arbitrary data to a single call via `CallOptions` and translate it into concrete call fields before the first model invocation. The hook fires inside `(*agent).prepareCall` so it covers both `Generate` and `Stream`; per-call `PrepareCall` overrides the agent-level default set via `WithPrepareCall`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds `SystemPrompt *string` to `AgentCall` / `AgentStreamCall`. When non-nil it overrides the agent-level system prompt for this call, so applications can drive system content from runtime sources (e.g. fetch from Langfuse inside `PrepareCall` based on `CallOptions`). `prepareCall` resolves the field against the agent default both before and after the hook: hooks see the effective value and may clear it back to nil to opt into the default, while downstream code can rely on the pointer being non-nil. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Lets applications wire runtime-sourced configuration into an agent call (Langfuse, feature flags, etc.)
without rebuilding the agent or constructing a fresh closure per call. Two pieces working together:
application data from the call site to a hook that runs inside (*agent).prepareCall. The hook can read
CallOptions and mutate the call fields before the first model invocation. Per-call PrepareCall overrides
the agent-level default registered via WithPrepareCall. Covers both Generate and Stream.
when non-nil. Combined with (1), the hook can fetch a system prompt from a remote source — typical Langfuse
flow:
Design notes
the hook sees the effective value (consistent with how MaxOutputTokens, Temperature, etc. are merged via
cmp.Or ahead of the hook). After, so a hook that clears the field (c.SystemPrompt = nil) cleanly opts back
into the agent default instead of leaving a nil pointer for downstream to deref.
Stream deref it once into a local and use that as the per-call baseline; the per-step comparison is now
against the call baseline rather than agentSettings.systemPrompt, so dynamic prompts and PrepareStep
overrides interact correctly.
any per-agent state at construction time); only per-call data needs the typed channel that CallOptions
provides.
Test plan
CallOptions, sees the pre-resolved SystemPrompt (= agent default), overrides it, model receives the new
system message.
downstream uses the agent default rather than panicking.
the field, no hook in play.
PrepareCall, and the step-level recreation guard compares against the call baseline (not the static agent
setting).
AgentCall field copy specifically (a hook-based test would mask a missing copy).
Resolves #224
CONTRIBUTING.md.