Setup token analysis for all functions #256
Merged
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.
Add Token Counting Utility
When invoking an LLM with all tools (ie
createSolanaTools(agentKit)
), all tools are passed every time a function call occurs, consuming a high number of tokens per request. As the number of tools increases, this can lead to breaking context windows, model decoherence, and increased costs.Here is an example trace from Langsmith showing 40,000 tokens being consumed to answer the question "who owns toly.sol". For context,
gpt-4o
has a max response size of 16,000 tokensThe goal of this PR is to setup a way to easily profile high token function calls, so developers can effectively pick & choose which tools they want to include in their agents.
Changes Made
This PR adds the following changes:
pnpm tool-summary
to print out a table of how many tokens each function uses in each respective framework (as a Solana Agent KitAction
& as a VercelCoreTool
)pnpm tool-summary:langchain
to print out a table of how many tokens each langchainTool
usesExample of
pnpm tool-summary
Here's example of
pnpm tool-summary:langchain
:Implementation Details
gpt-4o
tokenizer fromtiktoken
to count tokensTransaction executed by agent
Example transaction:
Prompt Used
Additional Notes
Future PRs should use this information to group similar tools together to minimize unused tokens.
Checklist