It's mostly up to the DM, but Xanathar's Guide to Everything has some optional rules
In Xanathar's, Chapter 2 has a Spellcasting section that begins with:
This section expands on the spellcasting rules presented in the Player’s Handbook and the Dungeon Master’s Guide, providing clarifications and new options.
That's a little unclear as to what's a clarification vs what's a new option, but given that identifying spells isn't covered at all in the PHB or DMG, then I tend to view identification of spells as an option rather than a clarification.
Identifying a Spell states (my emphasis):
Sometimes a character wants to identify a spell that someone else is casting or that was already cast. To do so, a character can use their reaction to identify a spell as it’s being cast, or they can use an action on their turn to identify a spell by its effect after it is cast.
The specifics for how this works are listed in the book, but I'm honestly not a big fan of it because of the action cost listed above.
This is especially true for counterspell which already uses up your reaction. So if you use your reaction as stated above, then you won't be able to actual counterspell, which is pretty unsatisfactory from a player's perspective.
How we've done it at my tables
At my tables, we've tried a couple different options with varying effect. I'm not sure which I like more, so I'm going to just go ahead and discuss what we've done and let others determine if they prefer one of the ones I've used or the Xanathar's method.
You've gotta guess
In this scenario, there is no way to identify in time. You are perceiving a caster at work and throw up a counterspell to stop them. It's up to the player to determine the level of the counterspell they want to use, and they take the risk of wasting it against a cantrip or requiring an ability check if it's higher than the spell slot level they've used.
Honestly, I kind of liked this one the best. It gave you a choice to make, and one with cost. It's an in-the-moment event and if you feel that the risk was worth the reward, then you go with it.
We tried allowing an Arcana check at no cost. Similar to the system in Xanathar's, but without the reaction cost. This gave the player's more info and the ability to decide if and when to counterspell.
What's kind of nice about this is that it does create a bit more of a level playing field between PC counterspellers and NPCs. Looking at the flip side, most player's don't begin their action with "I'm casting a generic spell", they tend to state the spell they're casting. This gives the DM info that they don't necessarily provide to the players in the same way.
Being able to know each and every spell cast as it's being cast seems a bit more fair, but it also removes a lot of the potential risk.
This option is perfectly fine as well, but I did kind of like the unknown risk of my first option.
What about working as a team?
Another potential option is have two players work together. One player uses their reaction to identify, and then once identified, the 2nd casts counterspell with the additional information.
And that type of action is also going to be table dependent. This question covers some of the issues revolving around speaking outside of your turn (during a reaction in this case.)
It comes back to being up to the DM/table to decide
Whether you use the method from Xanathar's (whether optional or 'actual') or one of the systems I've tried, it's all about having fun. What works for one table and everyone enjoys it won't necessarily be the same for another table.
Talk to your group and figure out what method works. If you don't like how it ends up, you can always change it after talking about it.