When using material3 TextField, you can leverage inputTransformation
:
import androidx.compose.material3.TextField
import androidx.compose.foundation.text.input.InputTransformation
val maxLength = 10
TextField(
state = rememberTextFieldState(),
inputTransformation = InputTransformation.maxLength(maxLength),
)