Skip to content

Utils Guide

LangCrew includes utility functions for common development tasks. These are primarily internal functions, but can be useful for advanced use cases.

Check if files are binary or text:

from langcrew.utils import is_binary_file, is_text_file
with open('document.pdf', 'rb') as f:
data = f.read()
if is_binary_file(data):
print("Binary file")
elif is_text_file(data):
print("Text file")

Count tokens for LLM models:

from langcrew.context.token_utils import count_message_tokens
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
# Create an LLM instance (required for token counting)
llm = ChatOpenAI(model="gpt-4o-mini")
messages = [HumanMessage(content="Hello world")]
token_count = count_message_tokens(messages, llm)
print(f"Tokens: {token_count}")

Simple Chinese/English detection:

from langcrew.utils import detect_language, detect_chinese
# Detect language
lang = detect_language("你好世界") # "zh"
lang = detect_language("Hello") # "en"
# Check for Chinese
is_chinese = detect_chinese("你好世界") # True

Generate unique message IDs:

from langcrew.utils import generate_message_id
msg_id = generate_message_id() # "1748438204041_a7k9"

Most users won’t need these utilities directly. They’re primarily used internally by LangCrew components. Focus on the main Agent, Task, and Crew concepts instead.

For complete API details, see the source code documentation in the langcrew.utils module.