Chainstay length, size of cogs, and drivetrain type, are the factors that dictate chain length.
The type of drivetrain affects chain length because different group sets have different methods for measuring the correct length. It’s not necessarily as simple as putting the chain over the largest cogs and adding two links, anymore. If you were installing a whole new drivetrain, (or just chainring the size of either the cassette or chainrings) it would be best to look up the service instructions for how to properly measure chains on your system.
Chainstay length is self explanatory- a bike with a longer distance between the rear axle and the bottom bracket will need a longer chain, all else being equal. On full suspension bikes, the chainstay is often longest in position of full travel, so that’s usually required to determine the appropriate chain length.
Cog size also determines chain length. As you can tell from the traditional method, an improperly sized chain will have problems when you try to use certain gear combinations. On many modern drivetrains, the rear derailleur guide pulley is not concentric with the RD cage pivot, so chain length also affects the derailleur’s ability to track the curvature of the cassette.
A major problem with running too short of a chain, is that shifting to larger cogs can damage the system. In theory, you can avoid damage by determining which rear cogs (if 1x) or which sprocket combinations are safe before riding (turning the cranks in a work stand, and watching the derailleur cage while carefully shifting from smaller cogs to larger ones) but that damage can happen quickly when you’re not thinking about it. I wouldn’t recommend riding like that unless you can eliminate that risk by locking out the larger cogs with the derailleur limit screws.
My recommendation for you is to get the links necessary to run your chain at the correct length. It’s not uncommon in bike shops to need more than one packaged chain for a bike, because tandems, and recumbents, and many hybrids and beach cruisers, require longer chains than what comes in a single box. When we open two chains for one bike, we hold on to the remainder of the second one, so we don’t waste two complete chains on every such bike- we just cut the extra links from the extra 9 (or 8, etc) speed chain in the drawer, and connect them using a connecting pin or quick link as appropriate. If I were in your position, I would ask around at your local bike shops for a few links from a spare 9 speed chain, or, consider buying a second chain, taking the needed links from it, and keeping it around for the next time you need a few extra links of 9 speed chain.