test_count_tokens_approximately_with_tools() — langchain Function Reference
Architecture documentation for the test_count_tokens_approximately_with_tools() function in test_utils.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 5be7afcb_ba57_76f7_bfe6_4355506f55bf["test_count_tokens_approximately_with_tools()"] 03f6a5ae_d57a_eb66_626a_b9e082b763ea["test_utils.py"] 5be7afcb_ba57_76f7_bfe6_4355506f55bf -->|defined in| 03f6a5ae_d57a_eb66_626a_b9e082b763ea style 5be7afcb_ba57_76f7_bfe6_4355506f55bf fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/tests/unit_tests/messages/test_utils.py lines 2913–2960
def test_count_tokens_approximately_with_tools() -> None:
"""Test that tools parameter adds to token count."""
messages = [HumanMessage(content="Hello")]
base_count = count_tokens_approximately(messages)
# Test with a BaseTool instance
@tool
def get_weather(location: str) -> str:
"""Get the weather for a location."""
return f"Weather in {location}"
count_with_tool = count_tokens_approximately(messages, tools=[get_weather])
assert count_with_tool > base_count
# Test with a dict tool schema
tool_schema = {
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather for a location.",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string"}},
"required": ["location"],
},
},
}
count_with_dict_tool = count_tokens_approximately(messages, tools=[tool_schema])
assert count_with_dict_tool > base_count
# Test with multiple tools
@tool
def get_time(timezone: str) -> str:
"""Get the current time in a timezone."""
return f"Time in {timezone}"
count_with_multiple = count_tokens_approximately(
messages, tools=[get_weather, get_time]
)
assert count_with_multiple > count_with_tool
# Test with no tools (None) should equal base count
count_no_tools = count_tokens_approximately(messages, tools=None)
assert count_no_tools == base_count
# Test with empty tools list should equal base count
count_empty_tools = count_tokens_approximately(messages, tools=[])
assert count_empty_tools == base_count
Domain
Subdomains
Source
Frequently Asked Questions
What does test_count_tokens_approximately_with_tools() do?
test_count_tokens_approximately_with_tools() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/messages/test_utils.py.
Where is test_count_tokens_approximately_with_tools() defined?
test_count_tokens_approximately_with_tools() is defined in libs/core/tests/unit_tests/messages/test_utils.py at line 2913.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free